C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 3rd, 2003, 12:29 AM
Liu Liu is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 6 Liu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Reading Text, then outputting.

I tried searching through the topics, but nothing really matched what I was looking for.

I must admit, i'm a newbie at this - i've only been at it for about a few weeks now, and we've been given a project to do - however, it's been hindered by one problem.

We need to read information off a text file, and then out put it onto the dos display and simulataneously on to an output text file.

Code:
#include <iostream>
#include <fstream>

using namespace std;
 
int main()

{
	ifstream inData;
	ofstream outData;

	inData.open("C:netpay.txt");
	outData.open("C:netpay.txt");

                inData.get();  //problem right here

	inData.close();
	outData.close();
	
	return 0;

}

The problem is that, after I get the information, how do I use it? As in, how can I get the data to actually transfer to another file, or to use it in another function? I also noticed that the data from the .txt disappears after running the program - how can I prevent this? I don't even think i'm using the proper 'get' function to get a whole line.

Thanks for the help in advance.

Last edited by Liu : July 3rd, 2003 at 12:50 AM.

Reply With Quote
  #2  
Old July 3rd, 2003, 01:09 AM
Liu Liu is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 6 Liu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
EDIT: scratch that.. still the same problem.

Last edited by Liu : July 3rd, 2003 at 01:34 AM.

Reply With Quote
  #3  
Old July 3rd, 2003, 10:03 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,824 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 1 h 9 m 26 sec
Reputation Power: 446
First, I admit that I'm much more familiar with standard C I/O functions than with iostreams, but I might still be able to point you in the right direction.

I believe that you want to use getline() instead of get:
istream& getline( char* pch, int nCount, char delim = '\n'_);

However, get() should have worked for you if you had given it the parameters it needed:
istream& get( char* pch, int nCount, char delim = '\n'_);

When you "get" something, you also need to put it somewhere. For that, you need to declare a character array, AKA a "buffer", to hold the data that you get. That array must be large enough to hold an entire line plus one character (that extra character being the null-terminator that marks the end of the string). In the two parameter lists given above, pch is the name of the buffer that you have declared and nCount is the maximum number of characters that you will read in (should be buffer size minus one). You do not need to include the third parameter, which defaults to "new-line".

After the get/getline, buffer now contains the line that was read in. Now you can output the contents of buffer to the display screen and to the output file. Then you get/getline the next line in the input file and repeat until you have read and processed the entire input file.

Reply With Quote
  #4  
Old July 3rd, 2003, 10:32 AM
helado helado is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 56 helado User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
You take file input just like screen input. I'm a little rusty, so check your book for correct syntax, but this looks about right. I did a quick google search for you to read if this doesn't sink in.

http://cplus.about.com/library/weekly/aa051802a.htm

eg:
Code:
ifstream fin;
ofstream fout;
int n;
char str[81];
char chr;

fin.open("myfile.txt");
if (!fin)
{
   //output error msg and exit
}

fout.open("results.txt", ios::app);
if (!fout)
{
   //output error msg and exit
}

// File streams are handled just like IO streams
fin >> n;
fout << n << "\n";
cout << n << endl;

// You can use the getline function to get a line of characters.  
// I set my limit to 80 to have room for null terminator
fin.getline(str, 80);
fout.write(str, 80);  //I'm a little rusty on this
cout << str << endl;



//or using get
while (fin.get(chr))
{
   fout.put(chr);
   cout << chr;
}


fin.close();
fout.close();

Reply With Quote
  #5  
Old July 3rd, 2003, 11:27 AM
Liu Liu is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 6 Liu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks for the help

After doing some late night reading after I made the post, I was straying towards using a buffer and using getline; so I came up with this:

Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
 
int main()

{
	char buffer[256];
	

	//declaration of file stream variables
	ifstream inData;

	//open files
	inData.open("C:input.txt");

	//executes if open fails.
	if (!inData)
	{
		cout<<"Open input file failure"<<endl;
		exit (1);
	}
	
	while(!inData.eof())
	{
   	inData.getline (buffer, 256);
	cout<<buffer<<endl;
	}

	//Close file
	inData.close();
	
	return 0;

}

However, the reason why I didn't want to use the getline function is because the text file has a mixture of names and numbers (how much they earn in $$); so I wanted to somehow store their names separately from storing the numbers; so I can work with the numbers. Is there a way of splitting the the information after you use getline; or is there another 'get' function where it gets a row of characters, and stops at a white space.

So it'd resemble something like a user input (which is really what i'm looking for):

Code:
cin>>a>>b>>c;

where the user input is:

first middle last

Last edited by Liu : July 3rd, 2003 at 11:51 AM.

Reply With Quote
  #6  
Old July 3rd, 2003, 11:58 AM
helado helado is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 56 helado User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
You can use getline. The parameters for getline are istream& getline (char* s, streamsize n, char delim );

edit: This would work the best (I found an old program to look at, look for example below):
Alternatively you may also just want to try inData >> str1 >> str2 >> str3 >> int1;
I haven't done this for awhile and I don't have any old code to look at atm or else I'd give you a definite yes/no. The below would work though.


Let's say your file contains something like this:
Calvin Henry Hobbes 2500.45

then you're code would look something like this:
Code:
char firstname[81], middlename[81], lastname[81];
float cash;

while(!inData.eof())
{
   	inData.getline (firstname, 80, ' ');
	inData.ignore(1);  // to ignore our space
        inData.getline (middlename, 80, ' ');
        inData.ignore(1);
        inData.getline (lastname, 80, ' ');
        inData.ignore(1);
        inData >> cash;
        inData.ignore(1); //ignore \n
        cout << firstname << " " << middlename << " " << lastname << " earns " << cash << " dollars." << endl;
}


or

Code:
char firstname[81], middlename[81], lastname[81];
float cash;

while(!inData.eof())
{
   	inData >> firstname;
        inData >> middlename;
        inData >> lastname;
        inData >> cash;
        cout << firstname << " " << middlename << " " << lastname << " earns " << cash << " dollars." << endl;
}



Would output:

Calvin Henry Hobbes earns 2500.45 dollars.

So yes, you can do exactly like a cin >> a >> b >> c;

Last edited by helado : July 3rd, 2003 at 12:12 PM.

Reply With Quote
  #7  
Old July 3rd, 2003, 12:20 PM
Liu Liu is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 6 Liu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thank you!

I don't know how I overlooked that inData>>a>>b>>c can be used like cin>>a>>b>>c - must've been the lack of sleep :\

Now I can get on with the rest of the program.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Reading Text, then outputting.


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway