|
-
Feb 3rd, 2003, 02:10 PM
#1
Thread Starter
Addicted Member
Read from a file to a string
Hello I am new to C++ and i have the new visual c++ .net
i was woundering if someone could let me know a simple way to read from a *.txt file and put it into a string or a string array one line at a time. if someone could give me some sample code that would be great!
Thanks ahead of time
-
Feb 3rd, 2003, 03:14 PM
#2
Member
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream fin;
string Buffer;
fin.open("FileName.txt");
if(fin.fail())
{
cout << "Error Opening File\n"; //if the file is not found then display error and exit
return 0;
}
while(getline(fin,Buffer))
{
cout << Buffer << "\n"; //reads through the file untill the end displaying everyline
}
return 0;
}
Make sure that in the folder where u save tihs code is the txt file, just change where it says "FileName.txt" to the name of the file
This code will read through the file collecting every line and printing it.
Hope this helps 
BTW im not sure if this will work with VC++.Net since i don't have it, but im guessing it should
Death is always smiling down on us, the only thing we can do is smile back
-
Feb 3rd, 2003, 03:42 PM
#3
Stuck in the 80s
Re: Read from a file to a string
Originally posted by Buy2easy.com
Hello I am new to C++ and i have the new visual c++ .net
i was woundering if someone could let me know a simple way to read from a *.txt file and put it into a string or a string array one line at a time. if someone could give me some sample code that would be great!
Thanks ahead of time
Do you search at all before you post? Check out the FAQ or do a search first. You'll be surprised at what you find, especially with a question like this.
-
Feb 3rd, 2003, 03:52 PM
#4
Lively Member
First off let me say I'm not trying to be rude or step on anyone's toes. But I've noticed this disturbing trend among forums lately where questions are repeatedly getting answered with "Do a search, read the FAQ". Most of these have rather unpleasant tones. Then when people do searches they end up with the first page being "do a search, read the FAQ" replies. Fortunately I haven't noticed it as bad here but just wanted to bring up the topic.
-
Feb 3rd, 2003, 04:13 PM
#5
Stuck in the 80s
Originally posted by Arawn
First off let me say I'm not trying to be rude or step on anyone's toes. But I've noticed this disturbing trend among forums lately where questions are repeatedly getting answered with "Do a search, read the FAQ". Most of these have rather unpleasant tones. Then when people do searches they end up with the first page being "do a search, read the FAQ" replies. Fortunately I haven't noticed it as bad here but just wanted to bring up the topic.
Seeing someone ask "How do I make a message box?!" everyday gets a little monotonous.
The reason why I said what I said was because, about two posts down, I asked a question pertaining to nearly the same thing. Which pretty much goes to prove that he/she didn't look very much before they posted.
I'm sorry if I offended you. Perhaps you could ignore me in the future? Just for reference, there is an ignore option in your User CP. Feel free to use it.
-
Feb 3rd, 2003, 05:26 PM
#6
Frenzied Member
Originally posted by RabidChimp
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream fin;
string Buffer;
fin.open("FileName.txt");
if(fin.fail())
{
cout << "Error Opening File\n"; //if the file is not found then display error and exit
return 0;
}
while(getline(fin,Buffer))
{
cout << Buffer << "\n"; //reads through the file untill the end displaying everyline
}
fin.close();
return 0;
}
In general it's a good idea to close the filestream also, especially when you have multiple files open.
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Feb 3rd, 2003, 05:42 PM
#7
Thread Starter
Addicted Member
Thanks for your reply it helped me out a lot.......also how do i make a message box? Just kidding
-
Feb 3rd, 2003, 06:03 PM
#8
Member
my bad i forgot to close it, *slaps himself*
Death is always smiling down on us, the only thing we can do is smile back
-
Feb 5th, 2003, 12:32 PM
#9
Thread Starter
Addicted Member
small problem
i have a small problem w/ this code...i used it as is to test it and i put a file in the directory of the *.exe file w/ the name filename.txt i even matched the text to exactly match what was posted i think it was FileName.txt. But it keeps giving me the error message like it can't read the file.....What could be wrong?
Thanks!
-
Feb 5th, 2003, 02:53 PM
#10
Member
What was the actuall error? Also Try putting the txt file where the .cpp file instead of in the Debug folder where the exe is.
Death is always smiling down on us, the only thing we can do is smile back
-
Feb 6th, 2003, 05:59 PM
#11
Thread Starter
Addicted Member
the error says that the file cannot be opened...not an error message but the build in message that displays when the file can not be opened or is not found (build into your code example)
why would it need to be in the dir w/ the .cpp file? just woundering? I will try it when i get home. and i wil get back w/U
-
Feb 6th, 2003, 06:02 PM
#12
Thread Starter
Addicted Member
that fixed it! what is up w/ that why did it not need to be in the dir w/ the .exe?
-
Feb 7th, 2003, 05:53 AM
#13
Monday Morning Lunatic
When you run it direct from VC++, it changes the working directory to your project directory, *not* the output directory. This is so that you can use the same files with both debug and release versions.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|