C++ char buffer pointer error - c++

I'm using a function that reads a spooled file and sets a buffer with the output.
The function returns OK state and sets readBytes correctly. It also notifies that the reading operation has reached the end of the file.
char* splFileContent = new char[3000];
ULONG readBytes;
int z = cwbOBJ_ReadSplF(splFile, splFileContent, 500, &readBytes, 0);
//z value is REACHED END OF FILE or OK if read but didn't reach the end of the file.
The trouble comes when trying to convert the char buffer to string, I'm getting "4Ä" as string value...
I convert the char buffer to string this way:
stringstream s;
s << splFileContent;
string bufferContent = s.str();
What I'm doing wrong?

It looks like splFileContent is binary content and not printable characters.
The start of the file may contain a BOM of some sort, e.g. unicode indicator. If it is, you should read in the BOM first and then the rest of the file.
Note: unless the file read function here adds a NULL, be sure to append one as well.

Related

Produce garbage with using of fread

I would like to read the data with fread from file. However, I encounter the problem of setting of NULL terminator.I think the the line (fileMem[fileSize] = 0;) should have been solved. However, I still get rubbish at the with I check the value of "fileMem". Would anyone help me to find the problem?
I followed other posts about setting the NULL terminator but just does not work
File *input = fopen(filePath, "r");
fseek(input, 0, SEEK_END);
auto fileSize = ftell(input);
fseek(input, 0, SEEK_SET);
char* fileMem = new char[fileSize+1];
fileMem[fileSize] = 0;// the NULL terminator problem should have been solved here
clearerr(input);
fread(fileMem, fileSize,1, input);
What is the problem with my code?
fread is reading more bytes than fileSize, because you are specifying a record size of fileSize, and asking it to read only one text record. It then overwrites the 0 at the end with actual data, so you get garbage.
fread returns the number of bytes it actually read, so you can allocate a bigger buffer, then use the return value from fread to determine how much of it is valid (and to set a null-terminator).
Since it is updating your data in this way, I also suggest changing the file type to binary ("rb" rather than "r" in call to fopen).
The reason this is happening is because fread performs translation of text when in text mode ("r" rather than "rb"), such as carriage returns and line feeds.
Assuming you are on Windows, I think the problem is that you are opening the file in text mode and using fread which is meant for binary mode. In text mode, what you read may not be exactly what is in the file. Windows text files have "\r\n" at the end of the file, but in text mode this two character combination is converted to a single character, "\n". So the fileSize you computed will be too large and so your null terminator will be in the wrong place.
To verify this, change your fread to be:
int nr = fread( fileMen, 1, fileSize, input);
Swapping the middle args, will have fread return the number of bytes read. If you look at the value of nr, it will be smaller than fileSize because of the line end translation.
This wouldn't be a problem on a *nix system since there is no translation there in text mode.
When you want to use fread to read the contents of a file, you must open the file in binary mode.
FILE *input = fopen(filePath, "rb");
^^
Otherwise, the size of the file you get by using
fseek(input, 0, SEEK_END);
auto fileSize = ftell(input);
will be greater than the number of characters that what can be read by fread.
If you have a CR and a LF, they will count as two characters by the above method but fread will read only one character. Hence, fread will read less than fileSize characters. You can also change the fread line to:
// Swap the middle arguments.
// The first argument is supposed to be the size of each object.
// The second argument is supposed to be the number of objects to read.
auto n = fread(fileMem, 1, fileSize, input);
if ( n != fileSize )
{
// Surprise
}
fileMem[n] = '\0';

Reading text file cause invalid character at buffer end

Reading a simple text file in c++ display invalid characters at the end of buffer,
string filecontent="";
ifstream reader(fileName);
reader.seekg (0, reader.end);``
int length = reader.tellg();
reader.seekg (0, reader.beg);
char *buffer=new char[length];
reader.read(buffer,length);
filecontent=buffer;
reader.close();
cout<<"File Contents"<<std::endl;
cout<<filecontent;
delete buffer;
return false;
but when i specify buffer length incremented by one ie
char *buffer=new char[length+1];
reader.read(buffer,length+1);
it works fine without invalid characters i want to know what is the reason behind this?
You read a string without terminating it with a trailing zero (char(0) or '\0'). Increase the buffer length by one and store a zero at buffer[reader.tellg()]. Just increasing the buffer size is not good enough, you might get a trailing zero by accident.

ifstream and oftream issue

Just this:
int size = getFileSize(path); //Listed below
ifstream fs(path, ios::in);
ofstream os(path2, ios::out);
//Check - both streams are valid
char buff[CHUNK_SIZE]; //512
while (size > CHUNK_SIZE)
{
fs >> buff;
os << buff;
size -= CHUNK_SIZE;
}
char* dataLast = new char[size];
fs>>dataLast;
os<<dataLast;
fs.close();
os.close();
//Found on SO, works fine
int getFileSize(string path)
{
FILE *pFile = NULL;
if (fopen_s( &pFile, path.c_str(), "rb" ))
{
return 0;
}
fseek( pFile, 0, SEEK_END );
int Size = ftell( pFile );
fclose( pFile );
return Size;
}
File at path2 is corrupted and less then 1 Kb. (initial file is 30Kb);
I don't need advices how to copy file, I am curios what is wrong about this example.
First an important warning: Never (as in really never) use the formatted input operator for char* without setting the width()! You open yourself up to a buffer overrun. This is basically the C++ version of writing gets() which was bad enough to be removed (not just deprecated) from the C standard! If you insist in using formatted input with char* (normally you are much better off using std::string), set the width, e.g.:
char buffer[512];
in >> std::setw(sizeof(buffer) >> buffer;
OK, with this out of the way: it seems you actually want to change two important things:
You probably don't want to use formatted input, i.e., operator>>(): the formatted input operators start off with skipping whitespace. When reading into char* it also stops when reaching a whitespace (or when the width() is non-zero when having read a much characters and still space to store a terminating zero; note that the set width() will be reset to 0 after each of these reads). That is you probably want to use unformatted input, e.g., in.read(buffer, sizeof(buffer)) which sets in.gcount() to the number of characters actually read which may be less then size parameter, e.g., at the end of the stream.
You probably should open the file in std::ios_base::binary mode. Although it doesn't matter on some systems (e.g., POSIX systems) on some systems reading in text mode merges a line end sequence, e.g. \r\n on Windows, into the line end character \n. Likewise, when writing a \n in text mode, it will be replaced by a line end sequence on some system, i.e., you probably also want to open the output stream in text mode.
Th input and output operators, when used with strings (like buff is from the libraries point of view), reads space-delimited words, only.
If you want to read chunks, then use std::istream::read, and use std::istream::gcount to get the number of bytes actually read. Then write with std::ostream::write.
And if the data in the file is binary, you should use the binary open mode.

strstr not able to search the string inside the data in octal format

I am trying to find a string in the given file (actually the file is tar file(please pay attention here) and i opened the file in notepad++ and took randomly a string from that opened file) and i stored that full tar file in a buffer and now i want to find the position of the string i copied using strstr function in the stored buffer.
The code to do is this(which is absolutely correct)-
char *compare= "_.png"; //suppose this is the copied string
//which is to be find out in buffer using strstr
char * StartPosition;
StartPosition = strstr (buffer,compare);
__int64 count=0;
MessageBox(m_hwndPreview,L"before the while loop",L"BTN WND6",MB_ICONINFORMATION);
while (StartPosition!=NULL)
{
MessageBox(m_hwndPreview,L"hurr inside the while loop",L"BTN WND6",MB_ICONINFORMATION);
MessageBoxA(m_hwndPreview,strerror(errno),"BTN WND4", MB_ICONINFORMATION);
count=StartPosition-buffer+1;
return 0;
}
and suppose if i have content of tar file in notepad like as below from where i copied this string stored in compare-
3_VehicleWithKinematicsAndAerodynamics_.000.png IHDR (here is some strange data which can't be copied and also there are lot of NULL but we have to find out the position of "_.png" so not so difficult in this case ).
The question is my code works fine until i store the data before the .png then i am able to find its position using strstr the problem is when i try to find out the string postion which is appearing after
`3_VehicleWithKinematicsAndAerodynamics_.000.png IHDR ...suppose here we have strange data (which is data block if we see the tar parser)...after this we have another file like..."3_VehicleWithKinematicsAndAerodynamics_.html"`
and if i want to find this "3_VehicleWithKinematicsAndAerodynamics_.html" using strstr then i am not able to find it due to strange data in between them.(because i think that those data are not recognized by the compiler and dut to that i am not able to access the file which is located after the strange data)
to make more clear see the location of file in tar file is as follows-
3_VehicleWithKinematicsAndAerodynamics_.000.png IHDR ............(its data blocl-which is strange contents if you open in tar file)....3_VehicleWithKinematicsAndAerodynamics_.000.html
i have to access the .html file using strstr . why it is not accessing it ?? any ideas ?? *
PLEASE GIVE THE ALTERNATIVES TO ACHIEVE IT..I am sure what i try won't work..
A C style string is a number of characters terminated by a zero-character (NUL character - the value zero, not the character '0'). This means that strstr will stop as soon as it hits such a byte.
One solution that is quite plausible is to simply write a function that searches through binary data based on it's length, not on a "terminating character".
Something like this (this still assumes that the str is a C style string):
char *find_str_in_data(const char *str, const char *data, int length)
{
int pos = 0;
int slen = strlen(str);
while(pos < length-slen)
{
int i = 0;
while(i < slen && str[i] = data[pos+i])
{
i++;
}
if (i == slen)
return data + pos;
}
return NULL;
}
If you really want to use strstr then you need to escape the string contained in buffer with '\0'. If you know the size of data that was put into the buffer (let's say, sizeOfData), than you could do something like this before you use strstr:
buffer[sizeOfData] = '\0';
Warning: if sizeOfData is equal to the size of buffer, then you will either need a greater buffer or overwrite the last character with '\0' (in the second case you should check the buffer tail manually, because the character you've overwritten could be one of the characters of sequence you are looking for).

Qt: c++: how to read a ".dat" file

I have a ".dat" file that contains "1"s and "-1"s as a sequence in a vertical representation (i.e.: each element is in a single line.).
I am trying to read the file as follow:
char buf[30];
QFile sequence("Sequences.dat");
sequence.open(QFile::ReadOnly);
for(int sym=0; sym<29; sym++){
char c = symbols[sym] = sequence.readLine(buf,sizeof(buf));
symbols[sym] = c;
}
sequence.close();
however, the result is nothing like my sequence as seen below:
what did I did wrong ?
Check the readLine API doc: the return value is the number of bytes read, while the line is read into the buf array, which is overwritten at each iteration. Note that the first symbol of the inspected array is a '\0' (empty string), probably because the last line of your file is empty.