Read in file with Linked List

closed account (jEb91hU5)
Hi. I'm having trouble being able to read in my input data to my code from a file. I have it organized: last, first id. I need to be able to later output it: id First Last. I'm not quite sure what to put in my while loop. I think I'm supposed to use pos = str.find(',') and str.erase(pos, n) . Any help would be appreciated.

1
2
3
4
5
6
7
8
9
10
  void readem()
{
	int i;
	ifstream inf("demo.dat");
	i = 0;
	while (!inf.eof())
	{
		//What goes here?
	}
}



Here are some test names:

Cruz, Terry 340
Temple, Shirley 112
Armstrong, Lance 725
Fey, Tina 202
Last edited on
How would the actual file look? Gives us a sample, please
closed account (jEb91hU5)
Do you mean the input file?
Yes
Oh, wait is

Cruz, Terry 340
Temple, Shirley 112
Armstrong, Lance 725
Fey, Tina 202


The input file?
closed account (jEb91hU5)
It's like I wrote the names.

1
2
3
4
Cruz, Terry 340
Temple, Shirley 112
Armstrong, Lance 725
Fey, Tina 202
closed account (jEb91hU5)
Sorry if I’m being impatient. I’ve just got a lot on my plate and I’m trying to get this code done in the next couple days. Anyone have any ideas?
1
2
3
4
5
string str;
getline(fin,str,',');
first = str;
cin >> last;
cin >> id;


Typos
Last edited on
closed account (jEb91hU5)
Thank you so much! Really appreciate it! I was hoping to get this part done tonight. :)
closed account (jEb91hU5)
Can you maybe explain what it is that its doing?
Your use of eof() is wrong to start with.
If you have 10 characters in a file, and read exactly 10 characters, then eof() will still be false.
It's only after attempting (and failing) to read the 11th character that eof() would be true.
So you always need to check the return result of the file read operation itself.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <iomanip>
#include <sstream>

int main()
{
  std::string line;
  // just replace std::cin here with your inf
  while ( std::getline(std::cin,line) ) {
    std::istringstream in(line);
    std::string lastname, firstname;
    int id;
    if ( getline(in,lastname,',') ) {
      while ( in.peek() == ' ' ) in.get();   // burn possible spaces after comma
      if ( getline(in,firstname,' ') ) {
        if ( in >> id ) {
          std::cout << "L=" << lastname
                    << ", F=" << firstname
                    << ", ID=" << id << std::endl;
        } else {
          std::cerr << "Failed at id" << std::endl;
        }
      } else {
        std::cerr << "Failed at space" << std::endl;
      }
    } else {
      std::cerr << "Failed at ," << std::endl;
    }
  }
  return 0;
}


$ ./a.out 
Cruz, Terry 340
L=Cruz, F=Terry, ID=340
Temple, Shirley 112
L=Temple, F=Shirley, ID=112
Armstrong, Lance 725
L=Armstrong, F=Lance, ID=725
Fey, Tina 202
L=Fey, F=Tina, ID=202
Flintstone Fred 42
Failed at space
Rubble,,Barney 33
L=Rubble, F=,Barney, ID=33
Flintstone,Wilma,thirty
Failed at id

How much error checking you have depends on how sure you are about the file format.
Yeah sure sorry about that I was in a hurry.

getline can be found in <sstream>, it’s a function that reads all the characters it encounters in the given stream (fin, my example file stream) until it hits the ending character, which in this case is ','. after using it, everything else can be read normally because there are no extra attached characters to the other data pieces.
closed account (jEb91hU5)
So were you able to put this in the while loop, or did you have to format it more like how Salem c formatted theirs? And are you saying fin is just your file name?
fin is the associated file stream, so std::ifstream fin; fin.open("fileName"); so inf and I think you could put it in a loop, but be careful with it I guess.
Topic archived. No new replies allowed.