|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#1 |
|
Member (5 bit)
|
C++ help in Fstream code
Hello, im trying to get the point to C++, I'm taking visual basic at school, but I've heard C++ is better, and i think it is. Anyways, i have some basic knowledge of the language, but i've been suffering with the Fstream header, on how to read text files. Im trying to get a program that reads a text file, so far i have:
#include #include #include using namespace std; int main() { ifstream ifs("filepath"); string line; while(getline(ifs,line)) { cout << "[ " << line << " ]" << endl; } system("PAUSE"); return 0; } The bold part i think shouldnt belong there, but anyways. I got it off the internet, since im just looking at code to get a hang of what does what, But im trying to do it so that im able to type in the file path and then compile it, and then it shows everything in the txt file, so far ive only gotten it to show part of it, and since im not an expert yet i dont really know where to go next, so can someone please give me code if not a snippet of the code so that the code could be complete, im looking for a program that reads a txt file within the console, thank you all, Ill be glad to whatever i get |
|
|
|
|
|
#2 |
|
Registered User
Join Date: Nov 2001
Posts: 1,965
|
You can read the path in a similar way to reading file lines:
Code:
string path; getline(cin, path); ifstream file(path.c_str()); |
|
|
|
|
|
#3 |
|
Member (5 bit)
|
Okay thanks, but could you please give me complete code, as in with the headers and all, So what I have is :
Code:
#include |
|
|
|
|
|
#4 |
|
Registered User
Join Date: Nov 2001
Posts: 1,965
|
Your first example reads the whole file:
Code:
#include iostream
#include fstream
#include string
using namespace std;
int main(void) {
string path;
getline(cin, path);
ifstream ifs(path.c_str());
string line;
while(getline(ifs,line)) {
cout << line << endl;
}
return 0;
}
|
|
|
|
|
|
#5 |
|
Member (5 bit)
|
yeah i know, im sorry, it was the wrong code, but the second one is the one im sticking to, agian, thanks for any help
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|