where is the mistake ?

Feb 19, 2013 at 2:08am
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 <cmath>
#include <cstdlib>
using namespace std;

int main (){
    char filename [50];
    ifstream asd;
    cin.getline (filename, 50);
    asd.open(filename);
    
    //to close the program if txt does not open
    if (!asd.is_open()){
                        exit(EXIT_FAILURE);
                        }
                        
    char word [50];
    asd >> word;
    while(asd.good()){
                      cout << word << " ";
                      asd >> word;
                      }
                      
    system ("pause");
    return 0;
}


program runs but i can't create txt files
Last edited on Feb 19, 2013 at 2:48am
Feb 19, 2013 at 2:21am
If you're trying to create a file, I presume you mean an output file.
I don't see any output file in your program.
Feb 19, 2013 at 2:50am
The program doesn't create text files, it reads them and displays the content.

Jim
Feb 19, 2013 at 3:21am
Then I don't understand your problem. In your OP you state "i can't create txt files".
Feb 19, 2013 at 3:23am
I think you mistook my answer for the OP's reply ;-)

Jim
Feb 19, 2013 at 3:27am
Thanks,
where do i need to put the name of the file to be read ?
Feb 19, 2013 at 3:33am
Now I'm confused.

What you have posted is a fully working program that reads a filename from the user and then opens that file and displays the content. I note that it doesn't prompt for the filename, it just immediately reads the input, so that may be confusing you when you run it - it may seem like it's not doing anything.

But is that what you expect the program to do? If it is, and it's not doing that, can you tell us exactly what the problem is?

If that's not what you expect it to do, what do you think it should be doing?

Cheers,
Jim
Feb 19, 2013 at 4:02am
Alright, i finally understood it.
I need to write the file to read on screen.
Thanks for your help.
Topic archived. No new replies allowed.