I'm trying to read this input from a text file but whenever I try to output it, it shows up as 00000.

Input:

IBM 100 93.2 98.6
MER 200 67.2 43.89
QQQ 300 78.9 70.0
C 200 35.78 50.02
CSCO 400 45.67 57.23

This is what I have so far:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
	string stockName;
	double numShares, buyPrice, currPrice;

    ifstream inFile;
	ofstream outFile;
    
	inFile.open("C:\\text1.txt");
	outFile.open("C:\\text2.txt");

    if (!inFile) 
	{
        cout << "Unable to open file";
    }
	
	inFile>>stockName>>numShares>>buyPrice>>currPrice;
	outFile<<inFile; //Checking to see if it works.

    inFile.close();
	outFile.close();
    
    return 0;
}

I'm trying to read this input from a text file but whenever I try to output it, it shows up as 00000.

Input:

IBM 100 93.2 98.6
MER 200 67.2 43.89
QQQ 300 78.9 70.0
C 200 35.78 50.02
CSCO 400 45.67 57.23

This is what I have so far:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
	string stockName;
	double numShares, buyPrice, currPrice;

    ifstream inFile;
	ofstream outFile;
    
	inFile.open("C:\\text1.txt");
	outFile.open("C:\\text2.txt");

    if (!inFile) 
	{
        cout << "Unable to open file";
    }
	
	inFile>>stockName>>numShares>>buyPrice>>currPrice;
	outFile<<inFile; //Checking to see if it works.

    inFile.close();
	outFile.close();
    
    return 0;
}

why two backslashes? Shouldn't it be C:\ not C:\\?
Also does this even work? outfile<<infile? I don't remember ever seeing that.
-Greywolf

Edited 5 Years Ago by Greywolf333: n/a

why two backslashes? Shouldn't it be C:\ not C:\\?
Can you check to see if a file is opened by just (!inFile)? I'm a bit rusty. I think you might have to do (!infile.is_open()) or (!infile.good()).
-Greywolf

Just tried it, looks like they all work fine without getting the error message.

Just tried it, looks like they all work fine without getting the error message.

Sorry I'm a bit rusty. I still don't think you can do outfile << infile. All that will get you is insert the value of "infile" to your outfile. That's why you're getting weird results. I think you need to transfer them from the variables.

Sorry I'm a bit rusty. I still don't think you can do outfile << infile. All that will get you is insert the value of "infile" to your outfile. That's why you're getting weird results. I think you need to transfer them from the variables.

Something like this? :

inFile>>stockName>>numShares>>buyPrice>>currPrice;
	outFile<<stockName<<numShares<<buyPrice<<currPrice;

Hmm.. I'm getting this as the output now: IBM10093.298.6 It's only showing the first row with no spaces for some reason

Something like this? :

inFile>>stockName>>numShares>>buyPrice>>currPrice;
	outFile<<stockName<<numShares<<buyPrice<<currPrice;

Hmm.. I'm getting this as the output now: IBM10093.298.6 It's only showing the first row with no spaces for some reason

You only read the 1st row. And it won't put spaces in for you unless you tell it.
You could put it in a loop:

while (inFile.good()){
  inFile >> stockname >> shares >> price >> curr;
  outfile << stockname << " " << shares << " " << price << " " << curr << endl;
}

If your goal is to simply copy a file, though, there's a better though more advanced way:

int size;
inFile.seekg (0, ios::end); //put the cursor at the end
size = inFile.tellg();  //tell how far the cursor is from the beginning.. this is the size
inFile.seekg(0, ios::beg);  //put the cursor back at the beginning so you can read normally

char* buffer = new char[size];  //now make an array thats as big as the file size

inFile.read(buffer, size);  //read the entire file into the buffer

outFile.write(buffer, size);  //write the buffer to the outfile stream

You only read the 1st row. And it won't put spaces in for you unless you tell it.
You could put it in a loop:

while (inFile.good()){
  inFile >> stockname >> shares >> price >> curr;
  outfile << stockname << " " << shares << " " << price << " " << curr << endl;
}

If your goal is to simply copy a file, though, there's a better though more advanced way:

int size;
inFile.seekg (0, ios::end); //put the cursor at the end
size = inFile.tellg();  //tell how far the cursor is from the beginning.. this is the size
inFile.seekg(0, ios::beg);  //put the cursor back at the beginning so you can read normally

char* buffer = new char[size];  //now make an array thats as big as the file size

inFile.read(buffer, size);  //read the entire file into the buffer

outFile.write(buffer, size);  //write the buffer to the outfile stream

Thank you! It works now.

This question has already been answered. Start a new discussion instead.