![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | |||
|
Programmer
Join Date: Oct 2005
Posts: 48
Rep Power: 0
![]() |
Read from text file
Contain inside "status.txt":
Quote:
int fexists (char fname[])
{
int failed;
ifstream infile (fname, ios::nocreate);
failed = infile.fail();
infile.close();
return (!failed);
}
void main()
{
char fileName[50] ="status.txt";
FILE *fptr;
char buf[200];
if (fexists(fileName))
{
fptr=fopen( fileName , "r" );
while (fscanf(fptr, "%s", buf) != EOF)
{
cout<<buf<<endl;
}
}
}ouput: Quote:
so that the output is like this Quote:
![]() |
|||
|
|
|
|
|
#2 |
|
Newbie
Join Date: Feb 2006
Posts: 8
Rep Power: 0
![]() |
hi,
use getline() function to read the lines. |
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Like ludesign said, you can do the following:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char str[1024];
fstream file_op("c:\\status.txt", ios::in);
if(!file_op.good())
{
cerr << "Error" << endl;
return 1;
}
while(!file_op.eof())
{
file_op.getline(str, 1024);
cout << str << endl;
}
file_op.close();
cout << endl;
return 0;
}
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|