CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2010
    Posts
    36

    Exclamation C++ arrays and files

    I am doing a project and I have to read from a file and input the values into an array. I know how to do this. But now we have to read from a file in put it in multiple arrays. The file that I'm reading looks like this:

    4
    Assign1 200 95
    Assign2 100 80
    Project 100 -1
    Midterm 100 -1

    How would I make it so that the names are put into one array, the 1st numbers in another, and the last numbers in another array?

    Code:
    Code:
    int readFile(char fileName[])
    {
      int i = 0;
      ifstream input;
    
      input.open(fileName);
    
      while (i != input.eof())
        {
          input >> grade[i];
          i++;
        }
      input.close();
    }
    How do I make it so the information is stored into 3 different arrays
    Last edited by prestonprice57; November 4th, 2010 at 10:37 PM.

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Help with c++ arrays and file reading

    Simply write three input >> statements reading the different data fields into different arrays, all of which are indexed by [i].

    BTW, the first line of your input file contains a record count. Either read it first and use it for the terminal condition of your loop, or read and discard it, and then read the records until you reach EOF. If you do neither of those, your program most likely gets screwed up.

    HTH
    Last edited by Eri523; November 4th, 2010 at 10:49 PM.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Join Date
    Oct 2010
    Posts
    36

    Re: Help with c++ arrays and file reading

    But how would I read the statements that are further in the file?
    I know for the words I can just put char name[i] and put those into an array but how do I get the two numbers on the same line into a different array

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Help with c++ arrays and file reading

    What about simply using this:

    Code:
      input >> an_array[i];
      input >> another_array[i];
      input >> yet_another_array[i];
    ?

    Whitespace is a default separator when reading from cin that way, and both ' ' and '\n' are whitespace.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  5. #5
    Join Date
    Oct 2010
    Posts
    36

    Re: Help with c++ arrays and file reading

    Ohhhh alright thank you! Sorry I'm dumb. >.<

  6. #6
    Join Date
    Oct 2010
    Posts
    36

    Re: Help with c++ arrays and file reading

    Actually when I do that my array keeps coming out as huge numbers. Like nothing is stored.

  7. #7
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Help with c++ arrays and file reading

    Oops! I failed to notice that you are using eof() the wrong way in your original post. If you opened the file successfully, eof() will return the boolean value false, which is converted to integer 0. Therefore, the terminal condition of the loop will be true before the very first iteration and as a consequence you actually read nothing.

    Using eof() that way isn't a good idea anyway: It will only become true after you actually attempt to read past the end of file, and thus you will read exactly one bogus record at the end. So you are better off to use the record count in the first line of the file as a criterion, which already was one of the options I suggested in post #2.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured