Question
Hi, this is a C++ question. I would like to know how to read a specific line in a txt file. For example, there is
Hi, this is a C++ question.
I would like to know how to read a specific line in a txt file. For example, there is only one number on the first line, I would like to read that number on line 1. For the second line, I want to read in 5 pieces of data for my struct. For line 3-40, I want to read 4 pieces of data for another struct. What functions should I use?I have learn
ifstream infile ;
infile.open("data.txt);
while(!infile.eof()) ; so far. I am pretty sure that I have missed something. Can I use a for loop to read the data. And then at line 41 , I have to read in 5 pieces of data for my struct again. For the next 37 lines, I have to read in 4 pieces of data . How should I set the loop ? So my data look like this
1
3 3 3 3 3
2 2 2 2
2 2 2 2
2 2 2 2
2 2 2 2
2 2 2 2
2 2 2 2
3 3 3 3 3
2 2 2 2
2 2 2 2
2 2 2 2
2 2 2 2
2 2 2 2
3 3 3 3 3
2 2 2 2
2 2 2 2
2 2 2 2
2 2 2 2
......
The way I read data is infile >> struct.a >> struct.b etc .
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started