fstream char array input question

This is a discussion on fstream char array input question within the C++ Programming forums, part of the General Programming Boards category; Hello, simple question. I have a 2D array thats going to be reading in a string of characters from an ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    7

    fstream char array input question

    Hello, simple question. I have a 2D array thats going to be reading in a string of characters from an input file everytime it runs through a loop. The only trouble is, I want the array to stop reading in characters as soon as a space is read in from the input file. Any feedback is much appreciated.

  2. #2
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Using fstream, if you use the file like cin, it should stop after any whitespace.

    Code:
    ifstream myfile("file.dat");
    myfile >> temp;
    You can also use getline so it separates only by spaces:

    Code:
    ifstream myfile("file.dat");
    myfile.getline(temp, n, ' ');

    Where temp is a character array of some size and n is size of temp.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    7
    Thanks for your quick reply. I am aware of these techniques, but it is implementing them with a 2-dimensional array that I am having trouble with.

  4. #4
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Well, how are you trying to access them? A 2-dimensional char array, isn't that just two character arrays? Go through one and then the other.

    [edit]
    gah, i'm forgetting terminology or something.
    [/edit]
    Last edited by neandrake; 12-07-2004 at 09:17 PM.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    7
    I'm accessing the names via typical ifstream. The array looks something like this:

    char name[5][15];

    It is to read in 5 names, each of a 15 character maximum length. In the txt file they are read from, the names are arranged like this:

    Name1 Name2 Name3 etc.

    I need the inFile thing to stop reading things into the array when it hits a space in order to make what is below

    for (i = 0; i < 15; i++) {
    cout << name[0][i]; }


    NOT look like "Name1 Name2 Nam" (15 characters of stuff).

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    char name[5][16]; //Room for string terminator
    for (int i=0; i<5; i++)
       inFile >> name[i];
    
    for (int i=0; i<5; i++)
       cout << name[i] << endl;
    Or using the string class:
    Code:
    #include <string>
    using namespace std;
    .
    .
    .
    string name[5];
    for (int i=0; i<5; i++)
       inFile >> name[i];
    
    for (int i=0; i<5; i++)
       cout << name[i] << endl;

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    7
    got it. Thanks a lot for the help guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  4. simple char array question..
    By Captain Penguin in forum C++ Programming
    Replies: 6
    Last Post: 10-15-2002, 11:28 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

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