tags:

views:

124

answers:

5

I'm trying to read from files created outside of the program, but am having some trouble. The program has the user create a file. Then it reads words from two .txt files created outside of the program, and then writes the words to the created file.

#include "std_lib_facilities.h"

int main()
{
    string word;

    cout << "Create file.\n";
    char name[20];
    cin >> name;
    ofstream ost(name, ios::out);

    cout << "Open first file.\n";
    char filename[20];
    cin >> filename;      
    ifstream ist(filename);
    while(ist >> word) ost << word << " ";
    ist.close();

    cout << "Open second file.\n";
    cin >> filename;
    ifstream isttwo(filename);
    while(isttwo >> word) ost << word << " ";
    isttwo.close();

    ost.close();

    keep_window_open();
}

However, when I open the created file in notepad, it comes out blank. Is this because reading into a string is impossible because the files being read were created separately? I'm not really sure. Any help is appreciated.

A: 

A potential problem is your use of cin instead of getline.

Imagine this input:

Open second file.
>>file one.txt

This obviously won't open the file you want, since cin stops reading once you hit a space.

Try explicitly opening name, such as:

ost.open(name);

I also don't like your implicit use of the >> operator in the while loops. Try this:

while (!ist.eof())
{
   getline(ist, word);
   ost << word;
}
Hooked
This is practice exercise code, no intent to implement error handling.
trikker
The cin object also does tricky things with whitespace, so you might want to use getline even if you don't think that is a problem.
Hooked
If you can actually answer my question that would be great, otherwise your answers are merely code optimization which is unnecessary for a simple exercise.
trikker
There isn't a whitespace issue. I've used cin with other programs when reading from files created in the program and it worked fine.
trikker
Well, did you try my while loop?
Hooked
The while loop is incorrect anyway.
anon
A: 

Maybe you should null terminate the filename[20] string . Like filename[20]={0}; before getting it from the standard input ; Then try printing out the filename on the console to check if the file name is OK .

sameer karjatkar
No, char* will always be null terminated unless you try to write to the last byte in the array.
Hooked
+1  A: 

The code is correct. Just make sure when you write the name of the first file and the second one you write their extensions as well. For example :

first.txt
second.txt
AraK
It worked correctly when I included file formats.
trikker
A: 

Trikker,

The code compiled with insignificant changes and I came across the same problem you are having, though actually it works. Here's what happened with me. It has to do with how you are running the program. The exe file is saved in ../ProjectDir/Debug directory. If you save the two input files in Debug directory, you should run the program from the command prompt, NOT from the IDE (assuming Visual Studio 2008). However, if you run it from the IDE, save the two input files in ../ProjectDir/ProjectDir. Once I did that everything was ok with me.

Just to be sure about this directory thing, I have printed the directory from which the program is being run from the IDE. Though, the prompt shows that it is running the exe from the Debug directory, the working directory was actually ProjectDir/ProjectDir.

Anyway, please, give the following code a shot and do let us know?

//#include "std_lib_facilities.h"

#include <fstream>
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    system("cd");
    //string word;
    char word[80];
    cout << "Create file.\n";
    char name[20];
    cin >> name;
    ofstream ost(name, ios::out);

    cout << "Open first file.\n";
    char filename[20];
    cin >> filename;      
    ifstream ist(filename);
    while(ist >> word) ost << word << " ";
    ist.close();

    cout << "Open second file.\n";
    cin >> filename;
    cout << filename;
    ifstream isttwo(filename);
    while(isttwo >> word) ost << word << " ";
    isttwo.close();

    ost.close();

    //keep_window_open();
}

This directory issue also probably explains why you didn't get anything, when tried to print out if it was reading anything at all. You probably should have checked if the input file creation was successful. To do that, just add ...

if ( ist.fail() ){
  cout << "file open failed\n";
}

after

ifstream ist(filename)
A: 

What happens if you use absolute paths? e.g. on Windows try c:\file.txt and so on.

Daniel Earwicker