Thread: File I/O and Arrays

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    7

    File I/O and Arrays

    I have a text document namelist.txt
    This file contains a list of names in this format:

    Aimee
    Alice
    Alicia
    Allison

    etc.

    Here's what I want to do.

    -Open the file for reading
    -Read the first line
    -Assign the first line "Aimee" to variable[0]
    -Read the second line
    -Assign the second line "Alice" to variable[1]
    -Continue until EOF.

    Here's what I've written so far.

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
      ifstream fin;                 
      char c[11];
      char names[50][11];
      int i=0;
      fin.open("namelist.txt", ios::in);
      
      if(fin.fail())
      {
        cout << "Error: Unable to open namelist.txt\n";
        exit(1);
      }
    
      while(!fin.fail() && !fin.eof())
      {
    //    cout << c <<endl;           
        fin.getline(c,11,'\n')>>names[i];
    //    strcpy(names[i],c);
        ++i;
      }
      
      fin.close();               
      system("PAUSE");
      return(0);
    }
    The problem is the "++i" line. It assigns the value to names[0] just fine, but then on the next loop it crashes. I don't understand why this is happening. Maybe I'm going about this the wrong way. Any help would be appreciated.
    Last edited by John Gaden; 08-15-2008 at 02:38 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    fin.getline(c,11,'\n')>>names[i];
    Huh? What does this line actually do [or what do you INTEND it 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.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    7
    It goes to namelist.txt through fin and reads a line to c, the buffer being 11 characters and the delim being a new line. It then passes it to names[i]. It seems to work for the first time around when i=0, but once i is increased the app just crashes.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by John Gaden View Post
    It goes to namelist.txt through fin and reads a line to c, the buffer being 11 characters and the delim being a new line. It then passes it to names[i]. It seems to work for the first time around when i=0, but once i is increased the app just crashes.
    But that is certainly not what the code you have written actually does. I _THINK_ what it does is that it reads a string into c, it THEN reads another line of text into names[i]. This is because getline returns (a reference to) the istream that was passed into it.

    I'm sure that's not what you want.

    If you add a "cout << c << " " << names[i] << endl;" after this line, then you will probably see that c contains your first name and names[i] contains the second name in your list.

    Also, you should not use eof to control the loop itself - it only gets set AFTER the input has tried to read past the end of file, which means that in most cases, you have one extra element in the input that is "uninitialized garbage". See the FAQ for discussion on this.

    --
    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.

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    7
    Thanks a lot.

    I understand what you were saying about my >> idea.
    Turns out strcpy was the correct way to go, the issue seemed that "names[50][11]" 50 was not enough. Stupid mistake.

    This is the code that ended up working.

    Code:
      while(!fin.fail())
      {       
        fin.getline(c,11,'\n');
        strcpy(names[i],c);
        cout<<names[i]<<endl;
        ++i;
      }

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by John Gaden View Post
    Thanks a lot.

    I understand what you were saying about my >> idea.
    Turns out strcpy was the correct way to go, the issue seemed that "names[50][11]" 50 was not enough. Stupid mistake.
    ALWAYS check limits in loops.

    --
    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.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    It doesn't have to be that complicated. Something as simple as this would work.
    Code:
      while(fin.getline(names[i],11,'\n'))
      {       
        cout<<names[i]<<endl;
        ++i;
      }
    To satisfy matsp, you might want to add this expression to the while loop.
    Code:
    while(i < sizeof(names) / sizeof(*names) && fin.getline(names[i],11,'\n'))
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File I/O with arrays
    By 20,000leeks in forum C++ Programming
    Replies: 32
    Last Post: 09-18-2006, 02:16 AM
  2. File i/o & int arrays
    By LordBronz in forum C++ Programming
    Replies: 2
    Last Post: 09-10-2006, 11:50 AM
  3. Arrays and File I/O
    By Darklighter in forum C++ Programming
    Replies: 3
    Last Post: 02-11-2006, 08:40 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. File I/O with 2D arrays
    By hypertension in forum C Programming
    Replies: 2
    Last Post: 02-04-2003, 08:47 AM