Sponsored Content
Top Forums Programming erase fread 's internal buffer ? Post 302387516 by Loic Domaigne on Saturday 16th of January 2010 02:02:37 PM
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.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
explain_fread_or_die(3) 				     Library Functions Manual					   explain_fread_or_die(3)

NAME
explain_fread_or_die - binary stream input and report errors SYNOPSIS
#include <libexplain/fread.h> void explain_fread_or_die(void *ptr, size_t size, size_t nmemb, FILE *fp); DESCRIPTION
The explain_fread_or_die function is used to call the fread(3) system call. On failure an explanation will be printed to stderr, obtained from explain_fread(3), and then the process terminates by calling exit(EXIT_FAILURE). This function is intended to be used in a fashion similar to the following example: size_t how_many = explain_fread_or_die(ptr, size, nmemb, fp); ptr The ptr, exactly as to be passed to the fread(3) system call. size The size, exactly as to be passed to the fread(3) system call. nmemb The nmemb, exactly as to be passed to the fread(3) system call. fp The fp, exactly as to be passed to the fread(3) system call. Returns: This function only returns on success, the number read or 0 on end-of-input. On failure, prints an explanation and exits. SEE ALSO
fread(3) binary stream input explain_fread(3) explain fread(3) errors exit(2) terminate the calling process COPYRIGHT
libexplain version 0.52 Copyright (C) 2008 Peter Miller explain_fread_or_die(3)
All times are GMT -4. The time now is 04:29 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy