I/O transferring data from input file to output file.

I am having trouble with coding this program. I am learning I/O and I am supposed to read an integer from an input file given by the user and expect that integer to correspond to presidents names in the same input file. I am supposed to format those names with last name first then middle then first name. However I am unsure how to do this because I keep receiving the error that my input file was not opened. Here is my code so far. Thank you!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <fstream>
#include <string> 
#include <sstream>
using namespace std;

int main()
{
ifstream in_stream;
ofstream out_stream;
string ifilename;
string ofilename;
string firstName;
string middleName;
string lastName;
cout << endl << "Enter the inputfile name:";
cin >> ifilename;
cout << endl << "Enter the outputfile name:";
cin >> ofilename;

in_stream.open(ifilename);
if (in_stream.fail())
{
   cout << "Input file opening failed.\n";
   exit(1);
}
out_stream.open(ofilename);
if (out_stream.fail())
{
   cout << "Output file opening failed.\n";
   exit(1);
}
int num = 0;
stringstream ss;
in_stream >> num;
ss << num << endl;
string president = ss.stri();
while (num >= 0)
//I am casting because I want the number to correspond to the 
//lines of string which are the president's names in the input file.
{
   in_stream >> firstName >> middleName >> lastName;
   president = lastName + " " + middleName + " " + firstName + "\n";
   cout << president; 
//I am trying to format the president name from last name to first 
//name
}
//For each name read the last, middle and first names and only write   
//the last name and first names in the output file  in the following
//  format:
//<last Name>,<first Name> *this is the last part of the program*

in_stream.close();
out_stream.close();

return 0;

So the expected output when the code is finished should look like this:

[input file] sample 1
3
Barack Hussein Obama
George Walker Bush
William Jefferson Clinton


[output file] sample 1
Obama, Barack
Bush, George
Clinton, William
Last edited on
Do you have an input file named ifilename in the proper location? Perhaps you meant to use the value held in the variable name ifilename not the string "ifilename"?

Please use code tags when post code.
Ok so I ask the user to create a file.. but when the user creates a file does it automatically go to the same directory that I am using to write the program? If it does then I don't understand why my input file is not opening? Also because I am asking the user to create a file, "ifilename" is not going to be the file name right? should I change 'string ifilename' to ofstream ifilename?
If it does then I don't understand why my input file is not opening?

It's probably because your file is not named "ifilename", this is the name of the file you're trying to open, not the name of the file that the user supplied. Try removing the quotation marks in your open() call.

Also using the C function exit() should be avoided in C++ programs. This C function doesn't properly call C++ class destructors and can lead to problems. Since these calls are all in main() just use a return statement instead.

[It's probably because your file is not named "ifilename", this is the name of the file you're trying to open, not the name of the file that the user supplied. Try removing the quotation marks in your open() call.]

Yes! your right! thank you so much. However.. .. now when I run the program it
displays infinite amounts of Obama's name on the screen! I had to exit out of putty. Do you know why that could be? Also I edited my program so you can see what the correct data in the files should be.
Last edited on
It would be much easier if you edited your post to add code tags around the code to make reading the code easier. At this point is would also be better if you posted your modified code in a new post, using code tags.

Ok I think I did it. hahah Sorry its my first time on cplusplus
Topic archived. No new replies allowed.