File I/O

This is a discussion on File I/O within the C++ Programming forums, part of the General Programming Boards category; I'm trying to read in a file which is formatted like so: Char_Array Int Char_Array Int . . . There ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    54

    File I/O

    I'm trying to read in a file which is formatted like so:

    Char_Array Int
    Char_Array Int
    .
    .
    .

    There is a single space in between the array and the int. How can I read in from this, and store them into proper variable (type char and int) to be manipulated by a function? They may be single or multiple digit integers.

    My thought is to use something like get, and store whatever is retrieved into the char array until the ' ' is met, and store the rest into the int until '\n', but not really sure how to achieve this.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Okay, so you mean this is a text file like this:
    Code:
    some_string 7
    etc 666
    I'm asking because you can store arrays, ints and other datatypes directly into a "binary" non-text file.

    If it is text, you can use ifstream::getline(), then apply sscanf():
    Code:
    char input[64]; // filled with getline
    char string[32];
    int x;
    sscanf(input, "%[^ ] %d", string, &x);
    Note this invloves C strings, not a C++ strings. %[^ ] reads all characters up to (but not including) a space.
    Last edited by MK27; 03-05-2010 at 08:04 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    54
    Yes, it is a text file.

    Specifically, it has a Name, then an ID.

    John 123
    James 456
    Joel 135

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    54
    How can I implement this using C++ syntax, instead of C?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Quote Originally Posted by mikeman
    How can I implement this using C++ syntax, instead of C?
    As in use functionality available via <fstream>? Before any answer is suggested, I would like to know if the name field can contain whitespace. If so, what separates the name field from the id field?

    Furthermore, suppose that the file's contents was:
    Code:
    John 123 James 456 Joel 135
    Would you like to report an error, or would you be fine with accepting this otherwise incorrect format?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    54
    The name field should not contain whitespace. The whitespace should be a delimiting character. Which I've sort of tried to implement using get, but haven't been very successful.

    As far as the second case goes, I suppose that would be an error. I'm doing this for a specific file, so it's not really necessary to be dynamic in that sense. After the name field is filled, I was thinking of using get again using the default '\n' delimiter. But then I'm not sure how to turn this from a char array into an int.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Okay, here's an idea: use std::getline() to read each line into a std::string object. Initialise a std::stringstream object using this std::string. Now, use formatted input with operator>> to read from this stringstream into a std::string (name) and an int (id), possibly ignoring trailing whitespace with the std::ws input stream manipulator. If the read failed or if eof() was not reached on the stringstream, report an input error. Otherwise, do as you will with the name and id, then go on to read the next line.
    Last edited by laserlight; 03-05-2010 at 01:13 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Nov 2008
    Posts
    54
    I'm not following too well.

    I get the first part to use getline.
    Code:
    getline(infile, temp)
    But I'm not too sure what you're meaning from here.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Something like this:
    Code:
    std::string line;
    while (std::getline(infile, line))
    {
        std::string name;
        int id;
        std::stringstream ss(line);
        if ((ss >> name >> id >> std::ws) && ss.eof())
        {
            // Use name and id.
        }
        else
        {
            // Handle incorrect line format.
        }
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Nov 2008
    Posts
    54
    Ok, in the file, % precedes a command, say insert, end, print, etc, to be done on a linked list.

    Here is what I did, but it's not working. Builds fine, but has error that doesn't allow it to run (force closes), not like endless loop. I simplified the file being read to get this working and at the moment it's just:
    Code:
    %INSERT
    MARK 129
    DAVID 205
    JOHN 440
    %PRINT
    So, when operational, it adds MARK, DAVID, and JOHN to the linked list with their respective ID.

    Code:
    if (strcmp(Next, "%INSERT")==0) {
    	while (infile.peek() != '%') {
    		getline(infile, line); 
    		stringstream ss(line);
    		if ((ss >> name1 >> ID >> ws) && ss.eof()) {
    			strcpy_s(Name, name1.c_str());
    			list.add_entry(Name, ID);
    		}//if
    	}//while
    }//if

  11. #11
    Registered User
    Join Date
    Nov 2008
    Posts
    54
    Nevermind, the error is happening elsewhere.

    Thanks for the help!

  12. #12
    Registered User
    Join Date
    Nov 2008
    Posts
    54
    .
    ..
    Last edited by mikeman; 03-06-2010 at 03:41 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-30-2004, 11:21 PM
  4. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 08:09 PM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 04:56 AM

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