简体   繁体   中英

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.

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

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

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 .

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM