![]() |
| | #1 |
| Registered User Join Date: Nov 2008
Posts: 12
| Help with reading from a file. 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);
Code: 9 890000088 888888088 888888088 800088088 808008088 808000088 808800888 000008888 888818888 Thank you for any help. |
| raptor1770 is offline | |
| | #2 |
| and the Hat of Guessing 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 | |
| | #3 | |
| Registered User Join Date: Nov 2008
Posts: 12
| Quote:
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 | |
| | #4 | |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Quote:
(Note also that most people consider the number after "0" to be "1", not "-1".) | |
| tabstop is offline | |
| | #5 | ||
| Registered User Join Date: Nov 2008
Posts: 12
| Quote:
Code: char_array[0][0] << file_name; Quote:
Thanks again for your help. | ||
| raptor1770 is offline | |
| | #6 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| |
| tabstop is offline | |
| | #7 |
| Registered User Join Date: Nov 2008
Posts: 12
| I just realized that instead of Code: char_array[0][0] << file_name; Code: file_name>>char_array[0][0]; Thank you so much for your help, this saved me tons of time. |
| raptor1770 is offline | |
| | #8 |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,357
| 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 offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |