Highlighted
Absent Member.
Absent Member.
5135 views

How to read content from a file and storing it in a string variable?

Jump to solution

I want to read the content of a file and store it in a string and use it as part of my script.

Right now i am doing it with the below code:

char Content[2936];

InPutFile=fopen(Filename,"r");
fread(Content,sizeof(char),2936, InPutFile);    
fclose(InPutFile);

I have manually checked the size of the file from properties and used the same (2936), which i want to avoid.

Is there any better way to do it? Especially when we don't know the size of the file.

 

 

 

0 Likes
1 Solution

Accepted Solutions
Highlighted
Acclaimed Contributor.
Acclaimed Contributor.

Re: How to read content from a file and storing it in a string variable?

Jump to solution

Hi

If you don't know the size of the file, you can always allocate the buffer larger. fread will return the size of the file read.

long fp, length; // for reading command output
char buffer[4096]=""; // allocate memory for the output of the command.

fp = popen("file_path_to_wanted_file","r");
length = fread(buffer, sizeof(char), 4096, fp);

And it can of course be done more dynamically, but this works and is pretty straight forward


br /ola
---------------------------------------------------------------------
Please mark post as solved if your problems or questions is/are resolved.
If this post was valuable to you, please consider kudo it.

.

View solution in original post

6 Replies
Highlighted
Acclaimed Contributor.
Acclaimed Contributor.

Re: How to read content from a file and storing it in a string variable?

Jump to solution

Hi

If you don't know the size of the file, you can always allocate the buffer larger. fread will return the size of the file read.

long fp, length; // for reading command output
char buffer[4096]=""; // allocate memory for the output of the command.

fp = popen("file_path_to_wanted_file","r");
length = fread(buffer, sizeof(char), 4096, fp);

And it can of course be done more dynamically, but this works and is pretty straight forward


br /ola
---------------------------------------------------------------------
Please mark post as solved if your problems or questions is/are resolved.
If this post was valuable to you, please consider kudo it.

.

View solution in original post

Highlighted
Acclaimed Contributor.
Acclaimed Contributor.

Re: How to read content from a file and storing it in a string variable?

Jump to solution

>char buffer[4096]="";

 

There is no need to initialize all 4K bytes with Nuls.

 

>length = fread(buffer, sizeof(char), 4096, fp);

 

Instead of hardcoding 4096, just use: sizeof(buffer)

(Provided it isn't a pointer.)

0 Likes
Highlighted
Acclaimed Contributor.
Acclaimed Contributor.

Re: How to read content from a file and storing it in a string variable?

Jump to solution

Hi Dennis

Of course you are right, sizeof(buffer) is far better to use!

The initialization do not set the whole memory area with nulls, it will only set it to a null terminated char array. To set the whole area with nulls you would need to use memset().

I have always initializing variables as a standard. This way you can check length, append it and so on without having to worry about if it has been really updated in a switch or is statement. Not initializing a char array will give what ever is there, can be nothing but could as well be garbage.


br /ola
---------------------------------------------------------------------
Please mark post as solved if your problems or questions is/are resolved.
If this post was valuable to you, please consider kudo it.

 

.
0 Likes
Highlighted
Acclaimed Contributor.
Acclaimed Contributor.

Re: How to read content from a file and storing it in a string variable?

Jump to solution

>The initialization do not set the whole memory area with nulls, it will only set it to a null terminated char array.

 

That is NOT what the C99/C++ Standards say.

If static storage duration, it is all initialized.  Function or block scope, every byte is initialized, if you initialize any.

(Assuming LoadRunner meets the C Standard.)

So you are better off just assigning the first byte: buffer[0] = '\0';

Unless dealing with security or you write the whole array and don't want random data.

0 Likes
Highlighted
Acclaimed Contributor.
Acclaimed Contributor.

Re: How to read content from a file and storing it in a string variable?

Jump to solution

Interesting, there is always something new to learn. 🙂

But the running point here is that all (automatic) variables needs to be initialized.

"In the absence of explicit initialization, external and static variables are
guaranteed to be initialized to zero; automatic and register variables have undefined
(i.e., garbage) initial values."
The C Programming Language 2nd Edition - Kerningham, Ritchie

This rule of thumbs is even more important to programmers not that experienced in C, like the thread creator.

Also if the time taken for clearing a 4k memory area is important, then I think we on a unsuitable Operating system 😉

.
0 Likes
Highlighted
Acclaimed Contributor.
Acclaimed Contributor.

Re: How to read content from a file and storing it in a string variable?

Jump to solution

>then I think we on a unsuitable Operating system

 

More of an issue for the hardware and not the OS.

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.