Opening & Printing a File

Hello,

So I have a task of:

-Writing a program that prompts the user for the name of a file and then opens that file and prints its contents (use getline).

-getline takes as its first parameter either cin or an input file, and a string as its second parameter. If a line was successfully read in it is placed into the string parameter, and returns true; otherwise false is returned and no value is placed into the string parameter.

-If the file doesn't exist, display and error message to that effect and prompt the user again for the file name.

This is what I have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

  1 #include <iostream>
  2 #include <fstream>
  3 #include <string>
  4 using namespace std;
  5 
  6 int main () {
  7 
  8 string line;
  9 ifstream infile(line.c_str());
 10 
 11 if (!infile) {
 12         cout << "Error reading data.txt" << endl;
 13         return -1;
 14 }
 15         cout<< "Enter a (string) line: ";
 16         if(getline(cin, line)); {
 17         cout << "you entered: " << line << endl;
 18         return true; 
 19 }    
 20       
 21         return false; 
 22     
 23 } 


Now, my concern is that my code isn't prompting the user for anything or opening it, I'm honestly really confused with my professor's instructions &, he's not at all helpful in guiding me to the right direction. Any advice would be appreciated!
The conflicting line-numbers don't help :)
Somewhere around line 9 (or is it 8), an empty string is created. Then on the next line that same empty string is used as the name of the file to be opened. When that instruction fails, the message at line 13 is output, and the program terminates.
Write a program that prompts the user for the name of a file

Where are you prompting the user for the name of the file?
I didn't do that, because I didn't understand how I should. I'll take care of line 15 because that was my attempt in doing it. I realize I'm wrong
@Chervill Thank you
Sorry, I missed the cin at line 16.

Don't you think you should prompt for the filename BEFORE you try and open the file?

closed account (D49iE3v7) Mar 12, 2013 at 7:22pm
I tried that @AbstractionAnon, & it works as it should doing so, but then it doesn't acknowledge any errors at all when I tested the output.

This is what I changed it per your suggestion:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  1 #include <iostream>
  2 #include <fstream>
  3 #include <string>
  4 using namespace std;
  5 
  6 int main () {
  7 
  8 string line;
  9 ifstream infile(line.c_str());
 10 
 11         cout<< "Enter a string line: ";
 12         if(getline(cin, line)); {
 13         cout << "you entered: " << line << endl;
 14         return true; 
 15 }       
 16  if (!infile) {
 17           cout << "Error reading data.txt" << endl;
 18           return -1;
 19   }
 20 
 21 }


Thanks for your input
I've re-formatted the code, the indentation was making it hard to follow.
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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
 
int main () 
{
    string line;
    ifstream infile(line.c_str());

    cout<< "Enter a string line: ";

    if (getline(cin, line))
        ;

    {
        cout << "you entered: " << line << endl;
        return true; 
    }   
    
    if (!infile) 
    {
        cout << "Error reading data.txt" << endl;
        return -1;
    }
}


One of the original problems remains. At line 10, an attempt is made to open the input file named "" (that is, an empty string for the name).

At line 14, there is an if statement which ends with a semicolon ( I put it on the next line for clarity). Remove the semicolon.

The code in braces on lines 17 to 20 will always be executed, thus the program terminates with the return statement at line 19 (though function main should return an int).
The return statement at line 19 is not needed.

What I suggest is that line 10, ifstream infile(line.c_str()); is moved after the user has entered the filename.

Also, it would be useful if the prompt explained to the user what they needed to do, so instead of cout<< "Enter a string line: "; I would put something like cout<< "Enter name of input file: ";
Last edited on
@Chervill Thank you so much for your time & advice, I'll go ahead & make the changes. I've much to learn..
Topic archived. No new replies allowed.