Highlighted
Absent Member.
Absent Member.
177 views

Regarding Reading large file in Chunks

Hi,

I am trying to submit a request where I need to read a large file and post it. But I am unable to do so with the below piece of code. So wondering if anyone could help me in reading a big file in may be 15k chunks and append all to one string and then post that string in the custom request.

char buffer[1000];
long file_stream;
char * filename = "C:\\Temp\\Sample.txt";


if ((file_stream = fopen(filename, "r")) == NULL )
{
lr_error_message ("Cannot open %s", filename);
}

while (!feof(file_stream))
{
fread(buffer, sizeof(char),1000, file_stream);
if (ferror(file_stream))
{
lr_output_message ("Error reading file %s", filename);
break;
}
}

if (fclose(file_stream))
lr_error_message ("Error closing file %s", filename);

lr_save_string( buffer,"loandata");
lr_output_message(" The buffer value is %s", lr_eval_string("{loandata}"));
0 Likes
3 Replies
Highlighted
Acclaimed Contributor.
Acclaimed Contributor.

Re: Regarding Reading large file in Chunks

What is the problem you are facing? The file reading seems to be ok. Did you face problem while submitting the request?
0 Likes
Highlighted
Absent Member.
Absent Member.

Re: Regarding Reading large file in Chunks

Hi,

For some reason, there are some weird special characters in between the strings while reading. I guess its happening every 1000 chars or something based on my declarations. As you see below, in the third line, there are some characters adding up.

Also using this code, its not reading till the end of the file. So can you run this and let me know where I could be going wrong.

Ex: My Name is <00001><001>My Name is <00001>
My Name is <00001><001>My Name is <00001>
My Name is <00ø û 001><001>My Name is <00001>
My Name is <00001><001>My Name is <00001>

I really appreciate for the help. Also find the attached file I am reading.
0 Likes
Highlighted
Absent Member.
Absent Member.

Re: Regarding Reading large file in Chunks

The problem is that your buffer size is 1000, and you are reading exactly 1000 characters to it. There is no space for the end of string character (\0).

Change the buffer declaration to:

char buffer[1005];

and it should work
0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.