Thread: Help with reading from a file.

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

    Help with reading from a file.

    Hello,

    I need to prompt the user for a filename and then open and read from that file. Also I need to be able to take the contents of the file I'm reading from and store them in a vector or stack.

    This is the code that I have so far but I don't know if this is the most efficient way to do it or why it doesn't seem to be working.

    Code:
            string file_name;
    
    	cout<<"What is the name of the maze input file?"<<endl;
    	getline(cin, file_name);
    
    	char c_file[20];
    
    	int i = 0;
    
    	while(file_name[i]!=0) //I convert the string into an array of chars because 
                                                //the open function only accepts char arrays as a  
    	{                                  //paremeter
    		c_file[i]=file_name[i];
    		i++;
    	}
    
    	fstream read_file;
    
    	read_file.open(c_file);
    The file to be opened is a simple text file containing a series of integers in this fashion.

    Code:
    9
    890000088
    888888088
    888888088
    800088088
    808008088
    808000088
    808800888
    000008888
    888818888
    From here I can't figure out how to actually pull information out of the file efficiently.
    Thank you for any help.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you have a string, you can use .c_str() to get a C-style string out of it without having to do the conversions yourself.

    You can use >> with files just as you can with anything else. (You'll need to be a little bit careful here; when you say a series of integers, do you mean you want to read "888818888" as one number, or as a sequence of characters? It will make a difference, both how you read and how you store.)

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    12
    Quote Originally Posted by tabstop View Post
    If you have a string, you can use .c_str() to get a C-style string out of it without having to do the conversions yourself.

    You can use >> with files just as you can with anything else. (You'll need to be a little bit careful here; when you say a series of integers, do you mean you want to read "888818888" as one number, or as a sequence of characters? It will make a difference, both how you read and how you store.)
    Thank you for the prompt response.

    What I meant is that I need to pull the integers out one at a time so that I can store them individually in a two dimensional array or vector. Imagine that the integers in the file will become a two dimensional grid.

    So the value of [0][0] == 9
    [0][-1] == 8
    [1][-1] == 8 and so on.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by raptor1770 View Post
    Thank you for the prompt response.

    What I meant is that I need to pull the integers out one at a time so that I can store them individually in a two dimensional array or vector. Imagine that the integers in the file will become a two dimensional grid.

    So the value of [0][0] == 9
    [0][-1] == 8
    [1][-1] == 8 and so on.
    So that means you need to not be reading integers, but instead be reading characters. (In other words, if your maze suddenly was made out of . and X and S and F, that wouldn't change how you read it in, right?) So you need an two-dimensional array (or vector of vectors I suppose) of characters.

    (Note also that most people consider the number after "0" to be "1", not "-1".)

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    12
    Quote Originally Posted by tabstop View Post
    So that means you need to not be reading integers, but instead be reading characters. (In other words, if your maze suddenly was made out of . and X and S and F, that wouldn't change how you read it in, right?) So you need an two-dimensional array (or vector of vectors I suppose) of characters.
    Ok, I understand that thank you. So to read characters from the file I should just read into a two dimensional array of characters somewhat like this?

    Code:
    char_array[0][0] << file_name;
    In some sort of loop?

    (Note also that most people consider the number after "0" to be "1", not "-1".)
    Lol I know but its just easier for me to think of the two dimensional array as a grid with X and Y coordinates with its origin at the first character. So moving down a line is moving -1 in the Y coordinate and moving to the right is +1 in the X coordinate.

    Thanks again for your help.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by raptor1770 View Post
    Ok, I understand that thank you. So to read characters from the file I should just read into a two dimensional array of characters somewhat like this?

    Code:
    char_array[0][0] << file_name;
    In some sort of loop?
    The file is always first, whether it's in or out.

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    12
    I just realized that instead of

    Code:
    char_array[0][0] << file_name;
    it should be

    Code:
    file_name>>char_array[0][0];
    and it seems to be working just fine.

    Thank you so much for your help, this saved me tons of time.

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by tabstop View Post
    If you have a string, you can use .c_str() to get a C-style string out of it without having to do the conversions yourself.
    The manual conversion isn't even correct, because nowhere does it say that std::string is null-terminated (although in practice it probably is, in order to make the .c_str() function efficient)
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM