C++ :: Read Binary File With Line Delimiter?
Oct 3, 2013
I want to read a binary file using as line separator "ff77" in order to parse further each line one by one with some regex since the file is big. I have a small ruby code shown below, but I'm new in C++, and I don't know how to replicate in C++ what this ruby code does.
Code:
#!/usr/bin/env ruby
BEGIN{ $/="xffx77" } # Line separator = FF77
File.open(ARGV[0],"rb") # Open in binary mode
[Code].....
View 14 Replies
ADVERTISEMENT
Nov 2, 2013
I run with debugger and appear this file referring the error to line 142 (in red):
Code:
/***
*xtoa.c - convert integers/longs to ASCII string
*
* The module has code to convert integers/longs to ASCII strings. See
*
*******************************************************************************/
#include <cruntime.h>
#include <stdlib.h>
#include <limits.h>
#include <tchar.h>
#include <internal.h>
#include <internal_securecrt.h>
#ifdef _UNICODE
#define xtox_s xtow_s
[Code] ...
In the calls stack window appears this
Code:
=>msvcr110d.dll!xtoa_s(unsigned long val, char * buf, unsigned int sizeInTChars, unsigned int radix, int is_neg) Line 142C
msvcr110d.dll!_itoa_s(int val, char * buf, unsigned int sizeInTChars, int radix) Line 176C
Get_Blocks.exe!main(int argc, char * * argv) Line 224C++
Get_Blocks.exe!__tmainCRTStartup() Line 536C
Get_Blocks.exe!mainCRTStartup() Line 377C
kernel32.dll!7695336a()Unknown
[Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]
ntdll.dll!76f19f72()Unknown
ntdll.dll!76f19f45()Unknown
It seems could be becuase to _itoa_s(), I'm using like this:
Code:
_itoa_s(CONVDEC(i), num, 10, 10);
sub += num;
View 5 Replies
View Related
Feb 23, 2014
I have attached the file that I need to read into a data structure. In the example I am just printing it to the screen. This following code has worked for me before, but now isn't. I have tried in visual studios, and on unix, and neither are running it. I ultimately need it to run on unix.
Example file:
word
book
programming
Here is my function to read in the file.
ifstream file;
string line;
file.open("words.txt");
if(file.is_open()){
while(!file.eof()){
getline(file, line);
cout << line <<endl;
}
}
file.close();
View 7 Replies
View Related
Jul 5, 2013
I have a text file (test.txt) with the following data:
01,05,25,20130728
01,06,25,20130728
01,07,25,20130728
01,08,25,20130728
01,05,25,20130728
01,05,25,20130728
01,05,45,20130728
01,05,65,20130728
01,05,85,20130728
01,05,35,20130728
01,05,25,20130728
01,05,35,20130728
I want to read this to one string called line. So far I have this code:
string line;
ifstream myfile ("/home/Test.txt");
if (myfile.is_open()) {
while (myfile.good()) {
getline (myfile, line);
for (int a = 0; a <= 335; a++) {
cout <<line.at(a);
} }
myfile.close();
}
so far its only printing the first line and then throwing an instance of 'std::out_of_range'
View 2 Replies
View Related
Oct 5, 2013
I need to read a text file which has various lines containing integers. I need to write those integers separately in a vector. Example, the first line of the text file contains 3 9 8 7 6 so vector[4]=3, vector[3]=9, vector[2]=8 and so on. Next read the second line 4 1 2 3 4 5 and write to another vector vector[5]=4, vector[4]=1...
I tried the code below but it will write from the second line, the whole line in one vector index.
int str; // Temp string to
cout << "Read from a file!" << endl;
ifstream fin("functions.txt"); // Open it up!
string line;
// read line count from file; assuming it's the first line
getline( fin, line );
[Code]...
View 1 Replies
View Related
Jul 2, 2013
I have an external file with one column of data. If I have a counter value let say counter =1, and counter++ and so on. How I can write such a c++ code that if the value of counter and value from the external file are same then generate an action let say cout both values i.e. value of counter and value from external file.
for more information, here is an example:
data in file(in one column): 2 6 8 9 10...
value of counter : 1 2 3 4 5 6 7 8 9...
then cout values only if value of counter and value from the file is same.
Here is my code so far, but it does not seem to work;
#include<iostream>
#include<fstream>
using namespace std;
int main() {
const int SIZE = 10; //Size declaration of array
int hours[SIZE]; //Array declaration
[Code]...
View 12 Replies
View Related
Jan 23, 2015
I am reading my file (20GB) line by line using boost like this
PHP Code:
boost::interprocess::file_mapping* fm = new boost::interprocess::file_mapping("E:Mountain.7z", boost::interprocess::read_only);
boost::interprocess::mapped_region* mr = new boost::interprocess::mapped_region(*fm, boost::interprocess::read_only);
char* bytes = static_cast<char*>(mr->get_address());
An exception is thrown in the second line while allocating memory for mr.
I use boost because it can also work in Mac which my code will be ported to.
View 3 Replies
View Related
Nov 19, 2013
How do i read the end of the line in a txt or dat file? I treid using fscanf in a while loop like this
Code:
while(fscanf(pfile,"%c") != EOF && fscanf(pfile,"%c") != '') {
char c = ' ';
fscanf(pfile,"%c",&c);
str += c;
}
The reason why its important to stop after each line is that i need the program to read the data from each line separately.
View 2 Replies
View Related
Jun 11, 2014
I have a .txt file which I want to read from and then write a new text file, this time with sorted lines. It is easy to sort one value, but what about sorting entire lines based on one value?
I want to sort the lines based on the FIRST value.
Example text file contents:
values;data
5.01 100
2.9 342
2.69 43534
10.1 43
6.8 45324
Output: Text file containing
2.69 43534
2.9 342
5.01 100
6.8 45324
10.1 43
It's easy to do this if there was only 1 number on each line, but I do I sort all the lines based on the first number in each line?
View 2 Replies
View Related
Dec 2, 2013
I have been working on a program to scanfile and whenever it encounters what the user wanted it prints it, and it is all right less the first line of the file that the program jumps,
Code:
#include<stdio.h>#include<string.h>
#include<stdlib.h>
void main()
{
unsigned int j=0 ;
char num[100] , str1[100], str2[100];
FILE *fp;
[Code]....
View 3 Replies
View Related
Aug 16, 2013
I am making a script to read the latest from a text file. It picks up the line by numbytes in fseek, but the data line may vary and numbytes not be accurate, how can I fix this?
And another problem is that the line has, date, time, value, separated by space, how to read that line and put these 3 information in variable?
#include <stdio.h>
#include <conio.h>
int main() {
FILE *arq;
char Line[100];
char *result;
int tam, i;
// Opens a file for READING TEXT
arq = fopen("temp.txt", "rt");
[code].....
View 19 Replies
View Related
May 3, 2013
I wrote the below code to find the line with "abc" as my parameter to strTosearch. I expected to the line 1st line but, this function always match the 2nd line. What am I missing here?
I wanted "found" to be "abc def hgi SSS".
Code in main()
String found=GetstringColSamLine("mytext.txt", "abc");
File name - mytext.txt
abc def hgi SSS
abc ppp yyy
string GetstringColSamLine(string routerFileName, string strTosearch)
[Code] ....
View 3 Replies
View Related
Jul 23, 2012
How to read a line with maximum of 8 bytes from a line in a file.
Let say line has 10248 bytes in one single line and same repeats for remaining lines in the file.
I need to read the first 8 bytes of a line and go on read the last 2 bytes of the line and repeat the till end of file.
View 3 Replies
View Related
Mar 7, 2014
How can a mulitline string be read line by line
ex: str = "PERIOD="week"
DAY="day"
TIME="time"";
View 2 Replies
View Related
Nov 24, 2014
When reading a binary .img why is it when you read it by slurping in the file, then save it to a string and cout, you don't see the binary but random garbage?
View 11 Replies
View Related
Jun 6, 2013
I build this function in my C++ code. I run the on a UNIX box, build HTML file to use in Windows.
In HTML file building code, I have only a string variable hold the contents of this text file (from unix) & add it into HTML file.
The issue is that when I view the HTML file on windows, it shows all the lines in the text file as one line. I think it lost the CRLF.
My question is when I add lines from text file (reading one line at a time) in UNIX, how do I maintain CRLF ( ) in the stringstream variable so that it will display on HTML in Windows properly, all the lines as it is in unix.
I am aware UNIX uses for line end.
string TextFileToString(string filename) {
string line;
stringstream dosString;
ifstream inFile;
[Code] ......
View 4 Replies
View Related
May 21, 2012
When I read a text file. I'm reading a list of strings in text file, with one string per line. The first line has extra characters in the string, the rest of the lines read are fine, and I can't understand where the extra characters come from. The file format is this...
A
AA
AAN
AAP
AAPL
AAWW
AAXJ
....
...
Code:
std::vector<std::string> strSymbolList;
std::string s = "";
std::ifstream infile( m_strFileSymbols );
while( std::getline(infile, s) ) {
strSymbolList.push_back(s);
}
infile.close();
View 3 Replies
View Related
Apr 22, 2013
I am trying to get the code to read from the txt file one bite at a time and then write this bite into the binary file but i cant seem to get it working.
FILE *fpcust, *fpcustbin; //<<<<<-----point to both sales and customers text files, and the new .bin files for both
char buffer;
int ch;
int ch1;
fpcust = fopen("c:customers.txt", "r"); //<<<<-----pointing to the file
fpcustbin = fopen("c:customers.bin", "wb"); //<<<<<-----pointing to the new binary file, opening in writing binary
[Code]...
View 3 Replies
View Related
May 6, 2014
I use the following function to read doubles from a binary file:
Code:
int64_t read_current(FILE *input, double *current, uint64_t position, uint64_t length)
{
printf("asked for %" PRIu64" samples
",length);
printf("reading from %" PRIu64 " to %" PRIu64 "
",position,position+length);
union{
double d;
uint64_t i;
}
[code]...
As long as position+length doesn't exceed the end of the file, all is well. However, if it does, weird things start to happen. In particular, the apparent length of the file (that is, the value of "read" after the function has finished) becomes dependent on "length"). I know that "position" is never past the end of the file.
View 12 Replies
View Related
Oct 12, 2014
I have this file want to read it source code [URL] ....
View 10 Replies
View Related
Nov 9, 2013
I'm trying to do file compression/decompression in C and I need to handle one bit at a time.
I currently try to do:
unsigned char byte = fgetc(fptr);
and later
byte >>= 1;
but the problem is that I have to use the first bit of the byte and then treat the next 8 bits as one byte. The byte usage keeps shifting over in this way. It's probably quite clear that I'm a bit lost.
View 1 Replies
View Related
Feb 26, 2012
Trying to write very simple code to read a 4 byte int from a file.
code:
int tester;
FILE *fp;
fp=fopen("/home/bdluser/skeleton.blx","rb");
fread(&tester,sizeof(int),1,fp);
printf("tested 1 byte read should be 1: %i",tester);
I have tried editing the binary file....it outputs similar large numbers
00000000000000000000000000000001 outputs 808464432
0x00000001 outputs 808482864
0x10000000 outputs 808548400
10000000000000000000000000000000 outputs 808464433