Rihards Sisojevs Rihards Sisojevs - 2 months ago 14
C++ Question

How to read number (int) values from char array

;)
I`am currently studying Files and File streams in C++

My problem is:

I need to write several numbers in file, and after read data from it - only that data, if it can be modeled (%) by 4.
The main issue is that after I read data from file, and put it in "buffer" array, all numbers values transform in char array as single characters --> "100" splits to '1', '0', '0', and "89" for example to '8' and '9'
How to pull them together once again, so characters will transform back in numbers (in 100 not in '1, '0', '0') and put them in new array?

P.s. please be simply as possible :) I`am still learning!

#include <iostream>
#include <fstream>
#include <stdlib.h>

using namespace std;

int main()
{
int numbers [] = {1, 100, 2, 98, 22, 12, 72, 16, 50, 51};
int array_size = sizeof(numbers)/sizeof(numbers[0]);

//Start of file writing
ofstream To_file ("C:\\CodeBlocks\\My_projects\\Test_KD_Files\\1stFile.txt");

cout<< "This numbers will be written in file: " <<endl;
for (int start = 0; start<array_size; start++)
{
To_file<< numbers[start] << " ";
cout<<numbers[start] << ", ";
}
cout<<endl <<endl;

To_file.close();
//End of file writing

//Start of file reading

char buffer [50];
int index = 0;

ifstream Read_it ("C:\\CodeBlocks\\My_projects\\Test_KD_Files\\1stFile.txt");
cout<<"Data from file: " <<endl;

while (Read_it.getline(buffer, 50))
{
Read_it.getline(buffer, 50); //all read data will be set in array "buffer"

cout<< endl <<buffer[index];

while (index<50)
{
if (buffer[index]%4 ==0) //check - does number can be devided by 4
{
cout<<buffer[index]; //display mostly messed numbers and characters
}
index++;
}

}

Read_it.close();

return 0;
}

Answer

getline continue reading even a white space but stops at end of line character. so getline it is not what you search for.

the trick is to use >> operator which stops reading when reaching a white space or newline. then we use atoi() to convert from character string to integer:

#include <iostream>
#include <fstream>
#include <stdlib.h>

using namespace std;

int main()
{
    int numbers [] = {1, 100, 2, 98, 22, 12, 72, 16, 50, 51};
    int array_size = sizeof(numbers)/sizeof(numbers[0]);

                    //Start of file writing
    ofstream To_file ("File.txt");

    cout<< "This numbers will be written in file: " <<endl;
    for (int start = 0; start<array_size; start++)
    {
    To_file<< numbers[start] << " ";
    cout<<numbers[start] << ", ";
    }
    cout<<endl <<endl;

    To_file.close();
                    //End of file writing

                //Start of file reading

    int NewNumbers[10];
    char buffer [50];
    int index = 0;

    ifstream Read_it ("File.txt");
    cout<<"Data from file: " <<endl;

    while(!Read_it.eof())
    {
        Read_it >> buffer; // to read until white space 
        NewNumbers[index] = atoi(buffer);
        index++;
    }

    for(int i(0); i < 10; i++)
        cout << NewNumbers[i] << ", ";

    cout << endl << endl;


    Read_it.close();

    return 0;
}
Comments