Need help on Reading from a file

This is a discussion on Need help on Reading from a file within the C++ Programming forums, part of the General Programming Boards category; Makes a lot of sense to me. Open file for reading. Loop until end of file. Read 4 bytes, store ...

  1. #16
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,594
    Makes a lot of sense to me.
    Open file for reading. Loop until end of file.
    Read 4 bytes, store in temp. Check the first char (index 0), if it's m or f. Set to appropriate pointers to the correct variables.
    Read 4 new bytes. Convert to double and add to the specific gpa using the pointer. Increase the amount of male or female occourances.
    Loop.
    Then divide female and male GPA by the number of occourances in the file.
    Format a string into ToDisplay and print it.

    What doesn't make sense? It should make sense. Unless you don't know about pointers? Or maybe you don't understand fopen/fread/feof? I tend to never use istream, so unfortunately it's the only way I know of. But feel free to modify the code. Use the docs to find what the functions does and change it to a similar code with your code.

  2. #17
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by Elysia View Post
    Makes a lot of sense to me.
    Open file for reading. Loop until end of file.
    Read 4 bytes, store in temp.
    That's not right, already. "Loop until end of file" means what? Something like this?

    Code:
    while(!feof(file))
    {
        ...
    }
    You shouldn't use feof() in a loop like that.

    Also, "Read 4 bytes, store in temp." What happened to the step in between, where you check if the reading of 4 bytes was successful or not? THAT'S how you detect EOF, not by calling feof() in a loop condition.

  3. #18
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,594
    feof works just fine usually and I did not test the code either, of course.
    The general idea is the same.
    I changed it a little. It might just work better. Maybe I've been away from file reading for too long.
    Last edited by Elysia; 10-21-2007 at 10:46 PM.

  4. #19
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by Elysia View Post
    feof works just fine usually and I did not test the code either, of course.
    feof() only returns true if you've attempted to read while at the end of file and FAILED. Only THEN will feof() return true. So if you are not checking the return status of all your read calls, you WILL miss the end of file and you WILL go wrong.

  5. #20
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,594
    I changed it already...
    Code:
    for(;;)
    {
    	fread(temp, 4, 1, f);
    	if ( feof(f) ) break;
    	...
    That works fine for me, so far as I remember and it shouldn't be a bad thing. Anything can fail, even istreams. And this is just a simple sample. No need for over-safe error checking.

  6. #21
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by Elysia View Post
    I changed it already...
    Code:
    for(;;)
    {
    	fread(temp, 4, 1, f);
    	if ( feof(f) ) break;
    	...
    That works fine for me, so far as I remember and it shouldn't be a bad thing. Anything can fail, even istreams. And this is just a simple sample. No need for over-safe error checking.
    Using it that way (and ONLY that way, directly after the call to fread()) is okay. Using it in the condition of the loop is not.

  7. #22
    Registered User lifeis2evil's Avatar
    Join Date
    Oct 2007
    Posts
    76

    Unhappy

    nope I don't know anything about pointers or anything like that I am a beginner

  8. #23
    Registered User lifeis2evil's Avatar
    Join Date
    Oct 2007
    Posts
    76
    I only know simple coding except the while looping and if-else reading from the file and calculations

  9. #24
    Registered User lifeis2evil's Avatar
    Join Date
    Oct 2007
    Posts
    76
    How would I get this to read each line by looping and then getting the average of the m if user picks m or get the average of f if the user picks f and then out put it to the screen?

    Code:
    inData >> gender >> gpa;
    
    while (m <= 1)
    {
          inData >> gender >> gpa;
          
          if (m <= 1)
          cout<<":kjnk"<<endl;
          else if (f >= 1)
          cout<<"hhg"<<endl;
          
          
                       }

  10. #25
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,594
    Code:
    char temp[5]; // Buffer to hold data that is read from the file
    char gender; // Specifies if the data applies to males or females, ie m or f
    double nFemaleGPA = 0, nMaleGPA = 0; // Holds the GPA for males and females
    UINT32 nFemaleNum = 0, nMaleNum = 0; // Counter of how many times GPA has been read from the file for male/female
    temp[4] = '\0'; // C strings always end with \0 and string functions expect it to, so let's add one to the end.
    
    FILE* f = fopen("indata.txt", "r"); // Open indata.txt for reading, text mode
    if (f == NULL); // Failed to open file - figure out why and toss our an error or something
    for(;;) // Loop indefinetly
    {
    	fread(temp, 4, 1, f); // Read 4 bytes from indata.txt, store in buffer temp
    	if ( feof(f) ) break; // If we've reached the end of the file, break out of the loop
    	gender = temp[0]; // Take the first character read from the file and put it into the gender varaible. Should be m or f.
    	fread(temp, 4, 1, f); // Read 4 more bytes from the file and store in temp
    	if (gender == 'm') // If the gender is male
    	{
    		nMaleGPA += strtod(temp); // Convert the contents of temp (the 4 read bytes which should be a number) to a double and add to nMaleGPA
    		nMaleNum++; // Increase the male entries counter by 1
    	}
    	else if (gender == 'f') // If the gender is female
    	{
    		nFemaleGPA += strtod(temp); // Same as above
    		nFemaleNum++;
    	}
    }
    fclose(f); // Close file
    
    nFemaleGPA /= nFemaleNum; // Average GPA. Divide the total GPA by the number of entries found in the file.
    nMaleGPA /= nMaleNum; // Average GPA
    
    char gendertodisplay;
    
    for(;;)
    {
    	cout << "Enter gender to show data for. Male (m) or Female (f):"; // Ask for gender to display
    	cin.get(gendertodisplay, 1); // Get one character from the standard input
    	if (gendertodisplay != 'm' && gendertodisplay != 'f') cout << "Invalid gender specified! Try again!\n"; // If the user didn't type m or f, then print error message
    	else break; // If the user DID type m or f, then break the loop and continue
    }
    
    char ToDisplay[1000];
    if (gendertodisplay == 'f') // If user chose female gender
    	vsprintf(ToDisplay, "Average female GPA: &#37;0.3f.", nFemaleGPA); // Copy a formated string to ToDisplay.
    else if (gendertodisplay == 'm') // If user chose male gender
    	vsprintf(ToDisplay, "Average male GPA: %0.3f.", nMaleGPA);
    cout << ToDisplay;
    This code should do, then. I find it very simple. Also take a look at vsprintf in the docs to see how it works. This will print the GPA in XX.YY.

  11. #26
    Registered User lifeis2evil's Avatar
    Join Date
    Oct 2007
    Posts
    76
    When I compile it, it gives me an error saying UINT32 is undecleared

  12. #27
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,594
    It's usually declared if you include <windows.h>. Otherwise you can add this:
    Code:
    #define UINT32 unsigned int

  13. #28
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Since this is the C++ forum, I would suggest that using fread() and related function isn't quite the right thing to do.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #29
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,594
    Right, unfortunately, it's the only thing I tend to use. I'm not familiar with anything else.
    But the code should show the general idea anyway.

  15. #30
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Right, unfortunately, it's the only thing I tend to use. I'm not familiar with anything else.
    But the code should show the general idea anyway.
    I want to screw in a screw to connect two bits of wood, and you show me how to use a hammer to nail something together - perhaps it will solve the same problem, but not always with the same result.

    Presumably, this is a task for a C++ programming course, and using C-style solutions SHOULD not give you a good mark.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 2 of 8 FirstFirst 12345678 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21