Pregunta

I want to copy contents in a text file to a string or a *char. It would be better if I can copy the file content to an array of strings (each line an element of that array). This is my code:

int main() {
   ifstream inFile;
   inFile.open("index.in.txt"); //index.in has in each line a name and at the end there is a "."
   char ab[11];
   int q=0;
   char *a[111];
   if (inFile.is_open()) {
      while (!inFile.eof()) {
         inFile >> ab; //i think i don't understand this line correctly
         a[q]=ab;
         cout<<a[q]<<endl;
         q++;
      }
   }
   else{ 
     cout<<"couldnt read file";
   }
   inFile.close();
   cout<<"\n"<<ab<<endl; //it shoud be "." and it is
   cout<<"\n"<<a[0]<<endl; //it should be "ion" but it gives me "."
   return 0;
}

All values in the a array are equal to the last line which is dot

¿Fue útil?

Solución 2

You are overwriting your only buffer everytime in inFile >> ab; You read a line in buffer and save the address of buffer somewhere. Next time you read next line in the same buffer and save the exact same address as second line. If you read back your first line you will end up reading updated buffer i.e. last line.

You can change your code to

#include <vector>
#include <string>
#include <fstream>

using namespace std;
int main() {
   ifstream inFile;
   inFile.open("index.in.txt"); //index.in has in each line a name and at the end there is a "."
   string ab; //char ab[11];
   int q=0;
   vector< string > a(111); //char *a[111];
   if (inFile.is_open()) {
      while (!inFile.eof()) {
         inFile >> ab;
         a[q]=ab;
         cout<<a[q]<<endl;
         q++;
      }
    }
    else cout<<"couldnt read file";
    inFile.close();
    cout<<"\n"<<ab<<endl; //it shoud be "." and it is
    cout<<"\n"<<a[0]<<endl; //it should be "ion" but it gives me "."
    return 0;
}

Better use std::string and std::vector instead of arrays.

Otros consejos

int main() {
   ifstream inFile;
   inFile.open("index.in.txt"); //index.in has in each line a name and at the end there is a "."
   std::vector< std::string > lines;
   std::string line;
   if (inFile.is_open()) {
      while ( getline( inFile, line ) ) {
         lines.push_back( line );
      }
   }
...

now lines is a vector of string, each is a line from file

#include <iostream>
#include <fstream>
#include <vector>
#include <string>

bool ReadFile(const std::string &sFileName, 
              std::vector<std::string> &vLines)
{
    std::ifstream ifs(sFileName);
    if (!ifs)
        return false;
    std::string sLine;
    while (std::getline(ifs, sLine))
        vLines.push_back(sLine);
    return !ifs.bad();
}

int main() 
{
    const std::string sFileName("Test.dat");
    std::vector<std::string> vData;
    if (ReadFile(sFileName, vData))
        for (std::string &s : vData)
            std::cout << s << std::endl;
    return 0;
}

In order to read a line you MUST use std::getline. The >> operator will only read a word, that is a sequence of characters ended by a whitespace.

See: http://en.cppreference.com/w/cpp/string/basic_string/getline

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top