简体   繁体   中英

Reading a text file in c++

string numbers;
string fileName = "text.txt";


ifstream inputFile;
inputFile.open(fileName.c_str(),ios_base::in);
inputFile >> numbers;
inputFile.close();
cout << numbers;

And my text.txt file is:

1    2    3    4    5

basically a set of integers separated by tabs.

The problem is the program only reads the first integer in the text.txt file and ignores the rest for some reason. If I remove the tabs between the integers it works fine, but with tabs between them, it won't work. What causes this? As far as I know it should ignore any white space characters or am I mistaken? If so is there a better way to get each of these numbers from the text file?

Use getline to do the reading.

string numbers;
if (inputFile.is_open())//checking if open
{
      getline (inputFile,numbers); //fetches entire line into string numbers
      inputFile.close();
}

When reading formatted strings the input operator starts with ignoring leading whitespace. Then it reads non-whitespace characters up to the first space and stops. The non-whitespace characters get stored in the std::string . If there are only whitespace characters before the stream reaches end of file (or some error for that matter), reading fails. Thus, your program reads one "word" (in this case a number) and stops reading.

Unfortunately, you only said what you are doing and what the problems are with your approach (where you problem description failed to cover the case where reading the input fails in the first place). Here are a few things you might want to try:

  1. If you want to read multiple words, you can do so, eg, by reading all words:

     std::vector<std::string> words; std::copy(std::istream_iterator<std::string>(inputFile), std::istream_iterator<std::string>(), std::back_inserter(words)); 

    This will read all words from inputFile and store them as a sequence of std::string s in the vector words . Since you file contains numbers you might want to replace std::string by int to read numbers in a readily accessible form.

  2. If you want to read a line rather than a word you can use std::getline() instead:

     if (std::getline(inputFile, line)) { ... } 

    If you want to read multiple lines, you'd put this operation into a loop: There is, unfortunately, no read-made approach to read a sequence of lines as there is for words.

  3. If you want to read the entire file, not just the first line, into a file, you can also use std::getline() but you'd need to know about one character value which doesn't occur in your file, eg, the null value:

     if (std::getline(inputFile, text, char()) { ... } 

    This approach considers a "line" a sequence of characters up to a null character. You can use any other character value as well. If you can't be sure about the character values, you can read an entire file using std::string 's constructor taking iterators:

     std::string text((std::istreambuf_iterator<char>(inputFile)), std::istreambuf_iterator<char>()); 

    Note, that the extra pair of parenthesis around the first parameter is, unfortunately, necessary (if you are using C++ 2011 you can avoid them by using braces, instead of parenthesis).

Your program does behave exactly as in your description : inputFile >> numbers; just extract the first integer in the input file, so if you suppress the tab, inputFile>> will extract the number 12345 , not 5 five numbers [1,2,3,4,5] .

a better method :

vector< int > numbers;
string fileName = "text.txt";
ifstream inputFile;
inputFile.open(fileName.c_str(),ios_base::in);
char c;
while (inputFile.good())     // loop while extraction from file is possible
{
    c = inputFile.get();       // get character from file
    if ( inputFile.good() and c!= '\t' and c!=' ' ) // not sure of tab and space encoding in C++
    {
      numbers.push_back( (int) c);
    }
}
inputFile.close();

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