trikker
trikker

Reputation: 2709

Reading data from file created from outside

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.

Upvotes: 1

Views: 719

Answers (5)

Daniel Earwicker
Daniel Earwicker

Reputation: 116714

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

Upvotes: 0

Rayhan
Rayhan

Reputation:

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)

Upvotes: 0

Khaled Alshaya
Khaled Alshaya

Reputation: 96889

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

Upvotes: 1

sameer karjatkar
sameer karjatkar

Reputation: 2067

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 .

Upvotes: 0

jkeys
jkeys

Reputation: 3955

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;
}

Upvotes: 0

Related Questions