Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 28th, 2006, 9:10 PM   #1
myName
Programmer
 
Join Date: Oct 2005
Posts: 48
Rep Power: 0 myName is on a distinguished road
Read from text file

Contain inside "status.txt":
Quote:
AA BB
CC DD
The program code:
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:
AA
BB
CC
DD
Press any key to continue
The program requiremet is read the text file line per line,
so that the output is like this
Quote:
AA BB
CC DD
Please help..
myName is offline   Reply With Quote
Old Feb 28th, 2006, 9:20 PM   #2
ludesign
Newbie
 
ludesign's Avatar
 
Join Date: Feb 2006
Posts: 8
Rep Power: 0 ludesign is on a distinguished road
hi,

use getline() function to read the lines.
ludesign is offline   Reply With Quote
Old Mar 1st, 2006, 2:36 AM   #3
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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
nnxion is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:07 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC