help with reading a file
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: help with reading a file

  1. #1
    Join Date
    Jan 2008
    Posts
    40

    help with reading a file

    I am working on reading the contents of a file that looks like this

    Larry 4 M 40
    Leslee 6 F 99
    Mary 9 F 39
    Hank 6 M 88
    Eden 7 F 71
    Drake 5 M 94
    Ava 4 F 28
    Seth 4 M 11
    Renaldo 3 M 77
    Carrie 8 F 68

    I have been able to read the data in and print it to the screen by doing this
    Code:
    cout << fixed << showpoint << setprecision(2);
    	//getting the information for the file
    	cout << "Please enter the full path to the file: ";
    	getline(cin, inputFileName);
    	//Open the input file.
    	inputFile.open(inputFileName.c_str());
    
    	//check to see that the file was opened
    	if(!inputFile.is_open())
    	{
    		cout << "Unable to opent the input file" << endl;
    		system("PAUSE");
    	}
    	//set up a while loop to read to the end of the file
    	while(inputFile.peek() != EOF)
    	{
    		getline(inputFile, line);
    		cout << line << endl;
    	}
    my problem is that I am trying to get the data into variable so that I can work with it. I need to add up the scores and average them, So I have taken them in this way

    Code:
    while(!inputFile.eof())
    	{
    		inputFile >> firstName >> age >> gender >> score;
    		cout << firstName << age << gender << score << endl;
    	}
    that was my test code to see if it is working but it's not printing anything on the screen. I should mention that I am a beginner and I have only been working with this code for 3 days. So if you help, please explain in newbie terms

  2. #2
    Join Date
    Jan 2008
    Posts
    40

    Re: help with reading a file

    I think I figured it out. I needed to add

    inputFile.clear();

    inputFile.seekg(0);

    before my next loop.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center