Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 09-23-2004, 11:09 PM   #1
Member (5 bit)
 
blankloner's Avatar
 
Join Date: Apr 2004
Location: California
Posts: 29
Send a message via AIM to blankloner Send a message via Yahoo to blankloner
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
blankloner is offline   Reply With Quote
Old 09-24-2004, 09:24 AM   #2
aym
Registered User
 
aym's Avatar
 
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());
Function c_str returns a C string, which is required by ifstream constructor.
aym is offline   Reply With Quote
Old 09-24-2004, 06:49 PM   #3
Member (5 bit)
 
blankloner's Avatar
 
Join Date: Apr 2004
Location: California
Posts: 29
Send a message via AIM to blankloner Send a message via Yahoo to blankloner
Okay thanks, but could you please give me complete code, as in with the headers and all, So what I have is :
Code:
#include 
#include 
#include 
#include 

using namespace std;

int main(int argc, char *argv[])
{
    string word;
    ifstream infile("/MyFile.dat");
    infile >> word;
    cout << word << endl;
    infile.close();
    
    system("PAUSE");	
    return 0;
}
But i want it to read the whole file, is it possible? so far ive only seen examples of how to read 2 or 3 lines, but is it possible to read a whole txt file? thank you for your previous help.
blankloner is offline   Reply With Quote
Old 09-24-2004, 07:34 PM   #4
aym
Registered User
 
aym's Avatar
 
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;
}
aym is offline   Reply With Quote
Old 09-24-2004, 11:42 PM   #5
Member (5 bit)
 
blankloner's Avatar
 
Join Date: Apr 2004
Location: California
Posts: 29
Send a message via AIM to blankloner Send a message via Yahoo to blankloner
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
blankloner is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 06:53 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2