large text file processing error [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
everyone.
The program as shown below isn't executed for large text file, for example, 30GB.
The program is for simply converting text file format.
Let me know how to solve the issue.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include </usr/include/pcl-1.7/pcl/io/pcd_io.h>
#include <pcl/point_types.h>
int main (int argc, char** argv)
{
int r,g,b;
if(argc!=3)
{
fprintf(stderr,"Usage:%s\n(1)Input_XYZRGB_filename\n(2)Output_PCD_filename\n",argv[0]);
exit(1);
}
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGB>);
std::ifstream ifs(argv[1]);
std::string buf;
for(size_t i=0; ifs && getline(ifs, buf); i++)
{
// std::cout << buf << std::endl;
std::istringstream is(buf);
pcl::PointXYZRGB pnt;
is >> pnt.x
>> pnt.y
>> pnt.z
>> r
>> g
>> b;
pnt.r= (uint8_t)r;
pnt.g= (uint8_t)g;
pnt.b= (uint8_t)b;
cloud->push_back ( pnt );
}
pcl::io::savePCDFileASCII(argv[2], *cloud);
return 0;
}

Make sure that your file input/output library supports large files. You can read the documentation. Also check the size of the file position parameter in the seek operation. In order to support large files it has to be 64 bit, not 32. In MS Visual C++, standard iostream seems does not support large files. But you can use other, low level input/output functions: _sopen_s, _read, _close, _lseeki64, etc. In gcc and mingw, you can use functions: _sopen, read, close, lseek.

Related

Why is my C++ code with file output not working? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 months ago.
Improve this question
I didn't got any errors, but my C++ code is still not working. It's really simple:
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
string a;
ofstream fout("char.out");
ifstream fin("char.in");
fin >> a;
fout << a;
return 0;
}
char.in after running:
uiui
char.out after running:
Did I missed anything simple in my code?
P. S. : I got Norton Antivirus and my project folder is missed from AutoCheck.
in fact for reading and writing you should open and close file but you didn't close.
Also you have two files where you have done writing from one file and reading from another file, I wonder how you expect to get the correct output.
this is how it should be :
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
string a;
ofstream fout("char.out");
// check if file is created
if(fout.is_open()){
// do writing in file
}
else
cout << "can not open file\n";
fout.close();
//-----------reading the file----------
// use the same file
ifstream fin("char.out");
if(fun.is_open()){
// do reading from file
std::cout << a << std::endl;
}
else
cout << "can not open file\n";
fin.close();
return 0;
}
And if you want to add a line of text to the end of the file, you must add:
ofstream fout("filename" , ios::app);

Reading from a .txt file to a vector of doubles in C++ [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I am trying to read values from a .txt file to a vector (which is a member of a class) in C++, but despite the .txt having around 1000 lines, the vector is of size 0. I inserted a 'cout', and I know the file is opened and closed. I'm not sure what I could be doing wrong for the code not to read the contents of the .txt.
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cmath>
#include "option_class.h"
using namespace std;
int main(){
double cprice = 0.0;
int i = 0;
string line;
ifstream is;
is.open("/Users/<USER>/Desktop/SPY.txt");
if (!is){
cout << "Unable to open file" << endl;
return(0);
}
while(!getline(is, line).eof()){
is >> cprice;
option1.price.push_back(cprice);
}
is.close();
cout << "Closing file" << endl;
}
Have you tried something simpler:
while (is >> cprice)
{
option1.price.push_back(cprice);
}
The operator>> will skip whitespace, which includes newlines. There is no need to read a line at a time.

How to redirect text to a file in C++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
i am trying to write a small automated program to calculate some values for me and output some text to a simple .txt file. do the redirection symbols < > & << >> work the same in C++ as they do in the command line for batch scripts? When i try to search how to redirect to a .txt file in C++. All of the examples, and tutorials i have found are presented in a manner that assumes IO on the console like the following.
cout::<<"show this text on the console";
cin::>> whatever you would call here to accept user input.
what i want to know is will it work to do it this way?
#include <string>
using namespace std;
int main()
{
int X = 0;
string zero = "touchPress 0 483 652\n";
if {
(X=0)
zero>>C:\test.txt;
x+5;
} return 0;
}
Your code does not work. I am not absolutely sure what is the desired behaviour, but this code writes the string zero to a file:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
int X = 0;
string zero = "touchPress 0 483 652\n";
ofstream myFile("C:\\Data\\test.txt");
//a condition which is always true
if (X==0)
{
myFile<<zero;
X + 5; //this is valid but useless
}
return 0;
}
#include <fstream>
int main(){
string zero = "touchPress 0 483 652\n";
std::ofstream fout("test.txt"); // creates new test.txt in folder where .exe is
fout << zero; //same as cout << zero;//but in the file
return 0;
}
fout as cout, i just reworked your barely alive program. is this what you wanted?

Input file lines to array errors [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I am trying to read a file with full path and get each line and put them into an array. my code is like this:
#include <fstream>
#include <iostream>
using namespace std;
void main(){
int Log[200];
int i;
For(int i=0; i<30; i++)
{
getline(/var/asl/data/audit/20130502/20130502-0611/20130502-61157-UYHEZX8AAAEAAAbKRvKAAAAC, line);
Log[i] = line;
cout << Log[i] < "\n";
}
}
but the below errors come to me and I do not how to solve them. Can anyone help me?
log1.cpp:7: error: :main must return int
log1.cpp: In function int main():
log1.cpp:12: error: expected primary-expression before int
log1.cpp:12: error: expected before token
Another question I have is that if I want to search a special character that is in the line that stored in arrays,(I mean search in an array) what can I do?
Thanks a lot dear users for your reply. I tried the code and it does not have any errors. But when i run it nothing happen. My file is not in text format. it is as like as Apache server logs format. Should it be in text format? The other question is if i put these line in arrays can i search a special value in it?
Thanks for your reply in advance.
Salam ,Try this:
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main(){
string line;
ifstream myfile ("example.txt"); //file address
string Log[200];
int i=0;
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line);
Log[i] = line;
i++;
cout << line << endl;
}
myfile.close();
}
return 0;
}

how can i read binary data in c++? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to read and write binary data in C++.I use from ofstream and ifstream classes but it can't read some chars like 9,13,32.If is there another way to read and write theme.
Open the file using the std::ios::binary flag and then use .read(buffer,length); and .write(buffer,length); rather than the streaming operators.
There are some examples here:
https://cplusplus.com/reference/istream/istream/read/
https://cplusplus.com/reference/ostream/ostream/write/
Here is a program that does this:
#include <iostream>
#include <fstream>
int main(int argc, const char *argv[])
{
if (argc < 2) {
::std::cerr << "Usage: " << argv[0] << "<filename>\n";
return 1;
}
::std::ifstream in(argv[1], ::std::ios::binary);
while (in) {
char c;
in.get(c);
if (in) {
::std::cout << "Read a " << int(c) << "\n";
}
}
return 0;
}
Here is an example of it being run in Linux:
$ echo -ne '\x9\xd\x20\x9\xd\x20\n' >binfile
$ ./readbin binfile
Read a 9
Read a 13
Read a 32
Read a 9
Read a 13
Read a 32
Read a 10
This is a basic example (without any error check!):
// Required STL
#include <fstream>
using namespace std;
// Just a class example
class Data
{
int a;
double b;
};
// Create some variables as examples
Data x;
Data *y = new Data[10];
// Open the file in input/output
fstream myFile( "data.bin", ios::in | ios::out | ios::binary );
// Write at the beginning of the binary file
myFile.seekp(0);
myFile.write( (char*)&x, sizeof (Data) );
...
// Assume that we want read 10 Data since the beginning
// of the binary file:
myFile.seekg( 0 );
myFile.read( (char*)y, sizeof (Data) * 10 );
// Remember to close the file
myFile.close( );