Thread: whose the c++ wizard

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    2

    whose the c++ wizard

    my objective is to simply read in characters and store it in a multidimensional character array.

    file format .. 50 lines of 80 characters
    example
    155157JOHN Q. PUBLIC 00015787
    155238HELEN GONE 00015687

    trying to read from a employee.dat file until the end of file, but keeps on getting segmentation error.

    int main()
    {
    ifstream data;
    char string[80];
    char s1[50][80];
    int i=0;

    data.open("c:/employee.dat",ios::in);

    if(!data)
    {
    cerr << "file cannot be opened! " << endl;
    exit(1);
    }

    while(!data.eof())
    {
    data.get(string,80,'\n');
    strcpy(s1[i],string);
    cout << s1[i] << endl;
    i++;
    }

    data.close();

    return(0);
    }

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    16
    Three things:

    1. I believe the segmentation error came from the fact that you used a forward slash after "c:" instead of a backward slash, because it worked after I changed it to a backward slah.

    2. I'm not sure if I'm doing something wrong, but the program didn't seem to want to open "c:\employees.dat", because I believe root directory used in a filestream is the folder that the file is in. Therefore, just put the .dat file in the same place as the program.

    3. The program did not work for reading in the lines properly when I test-drove it. I added a .ignore function, which seems to have cleared it up.

    Anyways, here's what it looks like; this should work:

    Code:
    int main()
    {
    
        ifstream data;
           
        char string[80];
        char s1[50][80];
    
        data.open("employee.dat", ios::in);
    
        if(!data) 
        { 
    
            cerr << "File cannot be opened!" << endl;
            system("PAUSE");
    
        } 
    
        for (int i = 0; !data.eof(); i++)
        { 
    
            data.get(string,80,'\n'); 
    
            strcpy(s1[i],string); 
    
            cout << s1[i] << endl;
    
            data.ignore('\n', 10);
    
        } 
    
        data.close();
    
        system("PAUSE");
    
        return 0;
    
    }
    Last edited by Gabu; 07-21-2002 at 09:37 PM.

  3. #3
    Registered User Unreg1stered's Avatar
    Join Date
    Jul 2002
    Posts
    25
    Code:
    .......
    if(!data) 
        { 
    
            cerr << "File cannot be opened!" << endl;
            system("PAUSE");
            return 0;                    // you need to exit the program or you'll get the error
    
        } 
    ........

  4. #4
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    You might wanna change this:

    while(!data.eof())
    {
    data.get(string,80,'\n');
    strcpy(s1[i],string);
    cout << s1[i] << endl;
    i++;
    }

    to this:

    i=0;
    data.get(string,80,'\n');
    while(!data.eof())
    {
    strcpy(s1[i],string);
    cout << s1[i] << endl;
    i++;
    data.get(string,80,'\n');
    }


    I think that .eof() checks if EOF is in the input buffer. Hence, you wanna input, make sure it isn't eof, then copy the data over. If you don't, you might get some garbage written at the end of the array.

  5. #5
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    Next time try using quotes That way everyone can read the code
    what does signature stand for?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 04-26-2007, 06:18 AM
  2. Centering Wizard?
    By JoeCoder in forum Windows Programming
    Replies: 7
    Last Post: 01-21-2004, 03:56 PM
  3. Bypassing Internet Connection Wizard?!?
    By jinxster72 in forum Windows Programming
    Replies: 1
    Last Post: 06-26-2003, 04:59 PM
  4. VC++6.0 Add member function Wizard
    By 7stud in forum C++ Programming
    Replies: 5
    Last Post: 04-05-2003, 08:48 PM