Input/Output from Txt File

+2 votes
asked Nov 8, 2019 by Olivia Rose (140 points)
I am trying to work on a project that pulls data from a Txt file. I am running into the problem of not being able to pull the information out of the accompanying txt file properly. The best I can do is pull it in as a string, but I need a way to pull the information in as variables for the program. Does anyone have any suggestions?

2 Answers

+1 vote
answered Nov 8, 2019 by Rohan Ghobade (520 points)
Use file pointer that pointing to that file then use getline to get line by line from that file then go for next operation  or first you count number of characters and dynamically allocate the memory and store file conten into that then use for your next operation
+1 vote
answered Nov 8, 2019 by gameforcer (2,950 points)
It depends on the language you're using but what you can do is first extract line by line from the file into the string and then you extract substrings from these lines (ex. for a string "I am 1 string." you want to extract "1" since it is your value) and then simply cast it into an integer/long/double, basically whatever type you need.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.