How can i read data untill end of line?I have a text file "file.txt" with this
1 5 9 2 59 4 6
2 1 2
3 2 30 1 55
I have this code:
ifstream file("file.txt",ios::in);
while(!file.eof())
{
....//my functions(1)
while(?????)//Here i want to write :while (!end of file)
{
...//my functions(2)
}
}
in my functions(2) i use the data from the lines and it need to be Int ,not char
Don't use while(!file.eof()) as eof() will only be set after reading the end of the file. It does not indicate, that the next read will be the end of the file. You can use while(getline(...)) instead and combine with istringstream to read numbers.
#include <fstream>
#include <sstream>
using namespace std;
// ... ...
ifstream file("file.txt",ios::in);
if (file.good())
{
string str;
while(getline(file, str))
{
istringstream ss(str);
int num;
while(ss >> num)
{
// ... you now get a number ...
}
}
}
You need to read Why is iostream::eof inside a loop condition considered wrong?.
As for reading until the end of the line. there's std::getline.
You have another problem though, and that is that you loop while (!file.eof()) which will most likely not work as you expect. The reason is that the eofbit flag is not set until after you try to read from beyond the end of the file. Instead you should do e.g. while (std::getline(...)).
char eoln(fstream &stream) // C++ code Return End of Line
{
if (stream.eof()) return 1; // True end of file
long curpos; char ch;
curpos = stream.tellp(); // Get current position
stream.get(ch); // Get next char
stream.clear(); // Fix bug in VC 6.0
stream.seekp(curpos); // Return to prev position
if ((int)ch != 10) // if (ch) eq 10
return 0; // False not end of row (line)
else // (if have spaces?)
stream.get(ch); // Go to next row
return 1; // True end of row (line)
} // End function
If you want to write it as function in order to call some where, you can use a vector. This is a function which I use to read such file and return integers element wise.
vector<unsigned long long> Hash_file_read(){
int frames_sec = 25;
vector<unsigned long long> numbers;
ifstream my_file("E:\\Sanduni_projects\\testing\\Hash_file.txt", std::ifstream::binary);
if (my_file) {
//ifstream file;
string line;
for (int i = 0; i < frames_sec; i++){
getline(my_file, line);
numbers.push_back(stoull(line));
}
}
else{
cout << "File can not be opened" << endl;
}
return numbers;
}
I am working on a program to read floating point number from an txt file and store in array, and I need to check are there any invalid input like character.
My code is:
int main() {
string line;
ifstream myfile("data.txt");
int size;
float* result;
if (myfile.is_open()) {
getline(myfile, line);
size = stoi(line);
result = new float[size];
for (int i = 0; i < size; i++) {
myfile >> result[i];
/*if ( (isdigit(arr[i])==0) ){
cout << "Invaild input." << endl;
return 0;
}*/
}
myfile.close();
}
else {
return 0;
}
}
The first line of the txt file is the size of the array and the second line is the contents like
5 //size
1 -2 9.2 4.7 -5.2 //content
How can I check that is there any character exist in the array like 1 -2 B 4.7 -5.2 //Invalid input ?
I try the isdigit function but it fail.
If you get an invalid input, reading will fail, and you can check this in the usual manner.
if (myfile >> result[i])
{
// Handle success.
}
else
{
// Handle failure.
}
I have given 2 solutions. One uses built in arrays and other uses std::vector.
Solution 1: Using built in array
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
int main()
{
std::string line;
std::ifstream inFile("input.txt");
//in case of using array, size must be fixed and predetermined
double arr[120] = {0.0}; //you can choose size according to your needs
if(inFile)
{
double i = 0;//this variable will be used to add element into the array
int count = 0;
while(getline(inFile, line, '\n'))
{
std::istringstream s(line);
//take input(from s to i) and then checks stream's eof flag status
while(s >> i || !s.eof()) {
//check if either failbit or badbit is set
if(s.fail())
{
//clear the error state to allow further operations on s
s.clear();
std::string temp;
s >> temp;
continue;
}
else
{
arr[count] = i;
++count;
//break out of the loop so we do go out of bounds
if(count >=120)//note 120 is the size of the array and you can change it according to your needs
{
break;
}
}
}
}
}
else
{
std::cout<<"file could not be read"<<std::endl;
}
inFile.close();
for(double i: arr)
{
std::cout<<"elem: "<<i<<std::endl;
}
return 0;
}
The output of solution 1 can be seen here.
Solution 2: Using std::vector
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
int main()
{
std::string line;;
std::ifstream inFile("input.txt");
std::vector<double> vec;
if(inFile)
{
double i = 0;//this variable will be used to add element into the vector
while(getline(inFile, line, '\n'))
{
std::istringstream s(line);
//take input(from s to i) and then checks stream's eof flag status
while(s >> i || !s.eof()) {
if(s.fail())
{
//clear the error state to allow further operations on s
s.clear();
std::string temp;
s >> temp;
continue;
}
else
{
vec.push_back(i);
}
}
}
}
else
{
std::cout<<"file could not be read"<<std::endl;
}
inFile.close();
for(double i: vec)
{
std::cout<<"elem: "<<i<<std::endl;
}
return 0;
}
The ouput of solution 2 can be seen here.
Important Note
The advantage of using std::vector over built in array(in this case) is that you don't have know the size of the vector beforehand. So it is preferable because you don't know how many integers are there in the input.txt file. std::vector can handle this correctly(dynamically). But when using built in arrays you must know/specify the size of the array beforehand. This in turn means you must know beforehand how many integers are there in the input.txt, which is not practical.
I'm new to c++ coding. I'm trying to write a function that opens specified ".txt" files(I fed up with coping/pasting multiple times).What I need to realize:
Specify filename;
read data and save to double(type) array;
return array;
As far as I understood, c++ can't return array, but it can return pointer. The problem is: how to use it? Any help will be appreciated. :)
P.S My draft code (it's working):
double arr[10];
fstream file;
file.open("input.txt");
if(file.is_open()){
while(file.good()){
for(int i = 0 ; i < 10 ; i++){
file >> arr[i];
}
}
file.close();
}else{
cout<<"[ERROR]: File \"input.txt\" wasn't found!"<<endl;
cout<<"[INFO]: Terminating program...";
Sleep(1000);
exit(0);
}
I dunno how to write as a function. Moreover I dunno how to use it
To start, try this:
std::vector<double> theFunction(const std::string &filename)
{
std::vector<double> arr(10);
std::fstream file(filename);
if (file)
{
for (int i = 0 ; i < 10 && file.good(); i++)
file >> arr[i];
}
return arr;
}
std::vector<double> result = theFunction("input.txt");
if (result.empty())
// Can not read the file
I am trying to write a basic binary VTK file to display some data using ParaView but I have some errors and I don't understand why. Here is my test code in C++:
#include <iostream>
#include <fstream>
double myarray[72] = {
0,0,0,1,0,0,2,0,0,3,0,0,4,0,0,
5,0,0,0,1,0,1,1,0,2,1,0,3,1,0,
4,1,0,5,1,0,0,2,0,1,2,0,2,2,0,
3,2,0,4,2,0,5,2,0,0,3,0,1,3,0,
2,3,0,3,3,0,4,3,0,5,3,0};
int main()
{
std::ofstream vtkstream("test01.vtk", std::ios::out | std::ios::trunc);
bool ascii = false;
if (vtkstream) {
vtkstream<<"# vtk DataFile Version 2.0"<<"\n";
vtkstream<<"Exemple"<<"\n";
if (ascii) {
vtkstream<<"ASCII"<<"\n";
vtkstream.close();
vtkstream.clear();
vtkstream.open("test01.vtk", std::ios::out | std::ios::app);
vtkstream<<"DATASET STRUCTURED_GRID"<<std::endl;
vtkstream<<"DIMENSIONS 6 4 1"<<std::endl;
vtkstream<<"POINTS 24 double"<<std::endl;
for (unsigned int i = 0; i < 72; ++i) {
vtkstream<<myarray[i]<<" ";
}
} else {
vtkstream<<"BINARY"<<"\n";
vtkstream.close();
vtkstream.clear();
vtkstream.open("test01.vtk", std::ios::out | std::ios::app | std::ios::binary);
vtkstream<<"DATASET STRUCTURED_GRID"<<std::endl;
vtkstream<<"DIMENSIONS 6 4 1"<<std::endl;
vtkstream<<"POINTS 24 double"<<std::endl;
for (unsigned int i = 0; i < 72; ++i) {
vtkstream<<myarray[i];
}
}
vtkstream.close();
} else {
std::cout<<"ERROR"<<std::endl;
}
return 0;
}
The ASCII file format works perfectly but the binary version produces the following error in ParaView:
Generic Warning: In ........\src\VTK\IO\vtkDataReader.cxx, line
1363 Error reading binary data!
Where is my mistake in the VTK format?
It seems that VTK assumes that binary files are written as big endian, whereas most PCs use little endian storage (see the bottom of page 2 of the VTK file formats document). Can you try swapping the byte order when writing binary data and see if this solves your problem?
See also this VTK users post, which is similar to this question.
My 50 cents. Here is the code that finally worked for me. Using byte swap, and using the function write to skip the formatting of the << operator
#include <iostream>
#include <fstream>
// Thanks to https://stackoverflow.com/questions/105252
template <typename T>
void SwapEnd(T& var)
{
char* varArray = reinterpret_cast<char*>(&var);
for(long i = 0; i < static_cast<long>(sizeof(var)/2); i++)
std::swap(varArray[sizeof(var) - 1 - i],varArray[i]);
}
double myarray[72] = {
0.001,0.002,0,1,0,0,2,0,0,3,0,0,4,0,0,
5,0,0,0,1,0,1,1,0,2,1,0,3,1,0,
4,1,0,5,1,0,0,2,0,1,2,0,2,2,0,
3,2,0,4,2,0,5,2,0,0,3,0,1,3,0,
2,3,0,3,3,0,4,3,0,5,3,0};
int main()
{
std::ofstream vtkstream;
vtkstream.open("test.vtk", std::ios::out | std::ios::app | std::ios::binary);
if (vtkstream) {
vtkstream<<"# vtk DataFile Version 2.0"<<"\n";
vtkstream<<"Exemple"<<"\n";
vtkstream<<"BINARY"<<"\n";
vtkstream<<"DATASET STRUCTURED_GRID"<<std::endl;
vtkstream<<"DIMENSIONS 6 4 1"<<std::endl;
vtkstream<<"POINTS 24 double"<<std::endl;
for (unsigned int i = 0; i < 72; ++i) {
SwapEnd(myarray[i]);
vtkstream.write((char*)&myarray[i], sizeof(double));
}
vtkstream.close();
} else {
std::cout<<"ERROR"<<std::endl;
}
return 0;
}
I'm having some trouble with replacing a portion of a file in binary mode. For some reason my seekp() line is not placing the file pointer at the desired position. Right now its appending the new contents to the end of the file instead of replacing the desired portion.
long int pos;
bool found = false;
fstream file(fileName, ios::binary|ios::out|ios::in);
file.read(reinterpret_cast<char *>(&record), sizeof(Person));
while (!file.eof())
{
if (record.getNumber() == number) {
pos=file.tellg();
found = true;
break;
}
// the record object is updated here
file.seekp(pos, ios::beg); //this is not placing the file pointer at the desired place
file.write(reinterpret_cast<const char *>(&record), sizeof(Person));
cout << "Record updated." << endl;
file.close();
Am I doing something wrong?
Thanks a lot in advance.
I don't see how your while() loop can work. In general, you should not test for eof() but instead test if a read operation worked.
The following code writes a record to a file (which must exist) and then overwrites it:
#include <iostream>
#include <fstream>
using namespace std;
struct P {
int n;
};
int main() {
fstream file( "afile.dat" , ios::binary|ios::out|ios::in);
P p;
p.n = 1;
file.write( (char*)&p, sizeof(p) );
p.n = 2;
int pos = 0;
file.seekp(pos, ios::beg);
file.write( (char*)&p, sizeof(p) );
}
while (!file.eof())
{
if (record.getNumber() == number) {
pos=file.tellg();
found = true;
break;
}
here -- you`re not updating number nor record -- so basically you go through all file and write in "some" location (pos isn't inited)
And Neil Butterworth is right (posted while i typed 8)) seems like you omitted smth