erase fread 's internal buffer ?


 
Thread Tools Search this Thread
Top Forums Programming erase fread 's internal buffer ?
# 1  
Old 01-16-2010
erase fread 's internal buffer ?

When fread is used twice, the second time it seems to overwrite the first time's output. Is there any way to clear fread's internal buffer after each use ?

Code:
char *FILEREAD(const char *FILENAME)
{
    static char READBUFFER[15001] = "";
READBUFFER[0] = '\0'; // try to solve the problem but this will not

    FILE *FILE1 = fopen(FILENAME, "r");

    fread(READBUFFER, 1, 15000, FILE1);

    fclose(FILE1);

    return READBUFFER;
}

int main(void)
{
printf("%s\n", FILEREAD("file1.txt")); // file content is: "wwwww" and FILEREAD outputs "wwwww"
printf("%s\n", FILEREAD("file2.txt")); // file content is: "aaa" and FILEREAD outputs "aaaww"

return 0;
}

# 2  
Old 01-16-2010
The problem isn't fread, but what you expect of it. According to the man page fread(3) it reads a certain amount of bytes from a file pointer, and copies them to a character array. It say nowhere that the target is cleared up front, or that it'll put a '\0' at the end, it leaves that to the programmer.

2 ways to solve this:
  1. memset(3) the array first
  2. fread returns the number of characters read. Put a '\0' behind that.
# 3  
Old 01-16-2010
When you call FILEREAD the first time, we have
Code:
READBUFFER[0]='w'
READBUFFER[1]='w'
READBUFFER[2]='w'
READBUFFER[3]='w'
READBUFFER[4]='w'
READBUFFER[5]='\0'

The second time, you set READBUFFER[0]='\0', but then the fread fills the first 3 elements with "aaa", so now
Code:
READBUFFER[0]='a'
READBUFFER[1]='a'
READBUFFER[2]='a'
READBUFFER[3]='w'
READBUFFER[4]='w'
READBUFFER[5]='\0'

What you need, is to set the byte past the amount of characters read by fread to '\0', or in your example READBUFFER[3]='\0'. Fortunately, fread() returns the number of items read, so:
Code:
size_t n;
...
n = fread(...)
READBUFFER[n]='\0';

should do the trick.

Cheers,
Loïc.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Malloc problem with fread() to read file to structure in C

Hello, I am trying to read a text file into linked list, but always got the first and last records wrong. 1) The problem looks related to the initialization of the node temp with malloc(), but could not figure it out. No error/warning at compiling, though. 2) The output file is empty,... (10 Replies)
Discussion started by: yifangt
10 Replies

2. Programming

What happens fwrite/fread at the same time?

Hello, I have a question about what happens when I copy the file which is being written by another process on Solaris 9/SPARC, UFS file system. in particular, I want to know what happens while some process is reading the file using fread or mmap, another process try to write something on the... (4 Replies)
Discussion started by: wipe3out
4 Replies

3. UNIX for Dummies Questions & Answers

Erase an at job

Hello everybody i need to erase a at job that i write. I wrote at 22 at>execute a command at>Ctr + d How do i erase this? I don`t wan`t to do this. I need to change something when i pressed ctrl + d give a job number (2 Replies)
Discussion started by: enkei17
2 Replies

4. Programming

fread: segementation fault(coredump) w/o stdlib.h

Hello All, I tried to test a sample fread example to read a complete file and the code is #include <stdio.h> #include <stdlib.h> int main () { FILE * pFile; long lSize; char * buffer; size_t result; pFile = fopen ( "test.xml" , "rb" ); if (pFile==NULL) {fputs ("File... (11 Replies)
Discussion started by: quintet
11 Replies

5. UNIX for Advanced & Expert Users

Forwarding internal internet packets to internal webserver using iptables

Hi, I need to redirect internal internet requests to a auth client site siting on the gateway. Currently users that are authenticated to access the internet have there mac address listed in the FORWARD chain. All other users need to be redirected to a internal site for authentication. Can... (1 Reply)
Discussion started by: mshindo
1 Replies

6. UNIX for Advanced & Expert Users

Linux FileSystem Internal Buffer size:

I know that Univ FileSystem stores all file data in the form of first few direct nodes followed by indirect nodes. But internally some systems implement where , a single block of 4096 isnt allocated alone a single block basis on physical drive, rather a large chunk of data is allocated and no. of... (1 Reply)
Discussion started by: GloriousDaisy
1 Replies

7. UNIX for Advanced & Expert Users

stty erase r

after hitting this command...on pressing r acts as a backspace character.... how to disable this function (5 Replies)
Discussion started by: bishweshwar
5 Replies

8. Programming

problem in reading file using fread

Hi All, These are the two ways i tried to read file but i getting work with second one not with the first. char buf; // Defining space for buf ctrlfnum = fopen(filename_arr.control_fname,"r"); 1) n = fread(buf,sizeof(buf),1,ctrlfnum); ============== (not works) 2) n =... (4 Replies)
Discussion started by: arunkumar_mca
4 Replies

9. UNIX for Dummies Questions & Answers

erase file

I have a file that always generated in the system eg. /tmp/log.txt , it is generated by the application program , but this file should not be present in the system otherwise there are some program problem , I want to erase this file once the program has generate it , as I know , it can link to... (2 Replies)
Discussion started by: ust
2 Replies

10. Programming

Using fread if the buffer size is not known

Hi... I am trying to read a binary data that have different types of messages of different lengths. I am using fread() but this functions needs the size and count to read the buffer from the file. I think this may cause that the buffer overlaps other messages. Is there an alternative to read... (1 Reply)
Discussion started by: jlrodz
1 Replies
Login or Register to Ask a Question