C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-02-2009, 04:02 PM   #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.
raptor1770 is offline   Reply With Quote
Old 07-02-2009, 04:05 PM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.)
tabstop is offline   Reply With Quote
Old 07-02-2009, 04:14 PM   #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.
raptor1770 is offline   Reply With Quote
Old 07-02-2009, 04:19 PM   #4
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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".)
tabstop is offline   Reply With Quote
Old 07-02-2009, 04:26 PM   #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?

Quote:
(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.
raptor1770 is offline   Reply With Quote
Old 07-02-2009, 04:31 PM   #6
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.
tabstop is offline   Reply With Quote
Old 07-02-2009, 04:33 PM   #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.
raptor1770 is offline   Reply With Quote
Old 07-02-2009, 06:02 PM   #8
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,379
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)
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is online now   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
opening empty file causes access violation trevordunstan C Programming 10 10-21-2008 11:19 PM
Formatting the contents of a text file dagorsul C++ Programming 2 04-29-2008 12:36 PM
airport Log program using 3D linked List : problem reading from file gemini_shooter C Programming 3 03-04-2005 02:46 PM
Possible circular definition with singleton objects techrolla C++ Programming 3 12-26-2004 10:46 AM
System drdroid C++ Programming 3 06-28-2002 10:12 PM


All times are GMT -6. The time now is 07:21 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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