D
D
danielsolosyatov2013-12-22 12:43:56
C++ / C#
danielsolosyatov, 2013-12-22 12:43:56

How to count characters from a file up to a space in C++?

Tell me how to read characters from a file up to a space in C ++? That is, there is a text file: 456 6546 2345 544 5654 ... etc. Need to count all characters up to each space, how to do it? There is a beginning of the code, it is desirable to build it into it.

void getText () {

    bool bRead = false;
    long length;
    long i, j;
    char * text;
    ifstream ifs("ggg.txt");
    if(!ifs)
        cout<<"Error jpen text.txt\n";
    else
    {
        ifs.seekg(0,ios::end);
        length = ifs.tellg();
        ifs.seekg(0,ios::beg);
        if(!(text = new char[length + 1]))
            cout<<"Allocation memory error\n";
        else
        {
            ifs.read(text,length);
            text[length] = '\0';
        }
        bRead = !ifs.bad();
        ifs.close();
        if(bRead)
        {
            for(i = 0; i < length; i++)
            {
                if(text[i] == ' ')
                <...>

Thanks in advance for suggestions.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergei Borisov, 2013-12-22
@risik

Are there integers in the file?
int a;
ifs >> a;
...

X
xandox, 2013-12-25
@xandox

wow you did something

void getText () {
     std::vector<int> numbers;
     int number;
     std::ifstream file("file_name.txt")
     if (!file) {
          std::cout << "error open file file_name.txt" << std::endl;
          return;
     }
     while(!file.eof()) {
         file >> number;
         numbers.push_back(number);
     }
}

Or did you want something else?

T
tsarevfs, 2013-12-22
@tsarevfs

So.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question