Getting text from text files

Hi everyone, I'm currently having a project where I'll have to get our data in text form from text files. The text file is aligned as so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Perth
82340.00
7
1.5

Melbourne
89982.70
5
1.5

Sydney 
39591
5
2.0

Kingston
13451
7
2.7


Given that I want to assign different variables to each line of text( names, and all the numbers below ). What should I do? I've been digging my brain towards this and I still have no idea. Can anyone help me out?
Here is the basic format I would try and follow

1. Open file
2. Verify file is open
Else notify user file is not found
// Since you have text, numbers and blank lines, you could check to verify that the input is the type data that you expect.

3a. while loop - read file while not EOF
4. getline 1st line (string)
5. read next 3 lines as double, int, double
6. verify each number is greater than 0 and less than max for that variable type.
8. perform math and output operations as desired.
3b. End while loop

7. close file.

You should be able to google each of those operations. such as
C++ input from text file
C++ getline
C++ read int from file
Please don't recommend
3a. while loop - read file while not EOF
- I get tired of explaining to people why their programs either don't work or are extremely fragile when they do that.
is it because eof() in a while loop actually never ends?...Then what is a better alternative to not eof()?
If I were you I'd follow samuelAdams' suggestion...at least give a solution or else you won't get the sort of feedback you're looking for
My apologies. It wasn't my intention to divert this thread off track. I was going to add a fuller explanation, but that would have sent it even further off track. Sorry about that.
I'd try using something like

1
2
3
4
5
6
while(getline(inf, stringVar)){
     inf>>floatVar;
     inf>>intVar;
     inf>>doubleVar;
     inf.ignore();
}


At least I remember something like that working for me... I may be forgetting something though... I'm at work and a bit sleep deprived haha :P

What are you supposed to store this information in? Are you supposed to use something specific like a vector or an array? Are you supposed to create your own struct or class? More info and I will try and help more :)
Last edited on

4. getline 1st line (string)
5. read next 3 lines as double, int, double


Can anyonhe tell me how to do this step for text files?
Can anyonhe tell me how to do this step for text files?

If you have used cin to get input from the keyboard, then you'll already be familiar with the syntax - though in any real-world examples there will always be little things to trip up the unwary.

To read a line typed by the user at the keyboard, you would have
1
2
    string line;
    getline(cin, line);

To do the same thing with a text file, just replace cin with the name of the input file stream.
1
2
3
    ifstream infile("input.txt"); // declare and open input 
    string line;
    getline(infile, line);


Similarly, to read numbers from the text file, put something like:
1
2
3
4
    double a;
    int b;
    double c;
    infile >> a >> b >> c;

Of course in your program you should choose better names for the variables rather than line, a, b and c.

There are a couple more things to consider if you want to do this repeatedly in a loop. Firstly, the >> extraction operator will ignore any leading whitespace, then extract as far as the end of the variable, but importantly it leaves any trailing whitespace (such as the newline character '\n') untouched. That can cause problems when using getline() which will stop reading after it finds a newline character.

There is also a blank line in the input file which separates each group of items. One way to deal with both trailing newline or a blank line is to use ignore(). Another is to use the ws manipulator which can actually be simpler to use but it gives rather less control and may not always be appropriate.

http://www.cplusplus.com/reference/istream/istream/getline/
http://www.cplusplus.com/reference/istream/istream/ignore/
http://www.cplusplus.com/reference/istream/ws/


Last edited on
Topic archived. No new replies allowed.