-1

I created a data file called program.txt. I need to create code that prints out number of lines and integer values from that program.txt
Heres the text I made

 1

 35
 45
 87

 9 

 100

the program.text has these values in it

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
  string calc;
  int test;
  int cool;
  int book;

  ifstream myfile;
  ifstream outof;
  myfile.open ("program.txt");
  outof.open("program.txt");

  if (myfile.fail())
  {
    cerr<<"Error Opening file"<<endl;
  }

  if(outof.fail ())
  {
    cerr<<"Error Opening file"<<endl;
  }

  while (!myfile.eof ())
  {
    getline(myfile,calc);
         ++book;      
  }

  cout <<book<<endl;

  while (!outof.eof ())
  {
    outof<<test;// 
    cool++;
  }

  cout<<cool<<endl;
  myfile.close();
  outof.close();
}

Also after cerr I tried exit (1) and it said exit was not defined. I am new to this any help would be greatly appreciated. Thanks This is C++ btw.

Himanshu
  • 4,327
  • 16
  • 31
  • 39
Adgj533
  • 1
  • 2

2 Answers2

0

The problem is that you are using ifstream, which stands for INPUT file stream. You want to use ofstream, which is OUTPUT file stream. You cannot write to an input file stream, hence why the << operator is not defined.

Also, rather than using exit(1) after your errors, you can just return 1; as you are inside your main function. This will terminate the program, returning 1 as the exit code.

If you really want to use exit, you need to #include <cstdlib>.

ajshort
  • 3,684
  • 5
  • 29
  • 43
  • ok i changed to ofstream, but now i have an error on line 46. It says no matching for call to 'getline" thats the only error im getting. again thanks so much for helping. – Adgj533 Nov 01 '15 at 03:30
  • Did you change both of them to ofstream? Your input needs to be ifstream, and output ofstream. – ajshort Nov 01 '15 at 03:32
  • Yeah I changed both to of stream, Im guessing you meant first one change to ifstream and second one has to ofstream? also i edited the original post to show line 46. ok now i made the corrections and run the build no errors but theres no output – Adgj533 Nov 01 '15 at 03:33
  • Yes. If you are OUTPUTTING to a file, you want OFSTREAM. If you you are reading INPUT from a file, you need IFSTREAM. Use the one that is appropriate for each variable. – ajshort Nov 01 '15 at 03:35
  • then in this case i would use ofstream for both right, Im only taking info from the file I made, not inputting anything am I correct? or no! – Adgj533 Nov 01 '15 at 03:36
  • which variable myfile or outof would be if and ofstream could u tell me pls, Im confused now! – Adgj533 Nov 01 '15 at 03:43
0

Your defined input and expected output aren't clearly defined, so I'm not sure what you're trying to do. However, here's a general idea:

Putting the filename in a fstream's constructor will automatically try to open the file for read/write. You dont need to call .open() anymore. Also, you shouldnt be reading and writing to the same file simultaneously if you dont know what you're doing.

std::ifstream myInputFile("program.txt");
std::ofstream myOutputFile("programOut.txt");

Rather than checking myInputFile.fail(), just use the overloaded boolean operator. In depth explanation: ifstream::is_open vs ifstream::fail?

if (!myInputFile) {
    //Something went wrong
    std::cerr << "Failed to open input file" << std::endl;
    return 1;
}

Define your std::string to hold lines as you read them, read all of your input file, and count the lines.

std::string line;
int lineCount = 0;
while (getline(myInputFile,line)) {
    ++lineCount;
    //Do something with 'line'
}

Maybe you'll need to store the lines from your input file so that you can output the count of the lines at the beginning of your output file, you might want to #include <vector> and do something like this:

std::string line;
std::vector<std::string> linesFromFile;

//Read in all lines of the input file and store them in the vector
while (getline(myInputFile, line)) {
    linesFromFile.emplace_back(line);
}

//All lines read, good time to close input file
myInputFile.close();

//Print number of lines read
myOutputFile << linesFromFile.size() << std::endl;

//Loop through lines and print them
for (auto &lineFromFile : linesFromFile) {
    myOutputFile << lineFromFile << std::endl;
}

//Done outputting, close output file
myOutputFile.close();
Community
  • 1
  • 1
Victor Stone
  • 498
  • 5
  • 18
  • im trying to print out how many lines and integer values that program.text file has. My friend from class helped me with this program. He got it right so he helped me we just changed the variables to random names! – Adgj533 Nov 01 '15 at 04:23
  • I made some corrections now Its running fine but no out put meaning output is blank any luck with that? see if you can run this code maybe its something im doing wrong. code seems perfect! – Adgj533 Nov 01 '15 at 04:29
  • Operation SUccess but Patient DIED! thats the state I am in now! :( RIP THE DREAM OF me being a PROGRAMMER! – Adgj533 Nov 01 '15 at 04:31
  • You cannot output to an "in file stream" (ifstream) – Victor Stone Nov 01 '15 at 05:52