LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-08-2002, 06:11 AM   #1
whepin
Member
 
Registered: Dec 2001
Location: thunder bay, on, Canada
Distribution: Windows vista & Ubuntu
Posts: 113

Rep: Reputation: 15
wrong with fprintf


char buf[2048];
int i=1;

fin=fopen(argv[1],"r");
fout=fdopen(1,"w");
memset(buf,0,sizeof(buf));
while(fread(buf,sizeof(char),2047,fin)>0)
{
buf[strlen(buf)-1]=0;
fprintf(fout,"ret times=%d,%s\n",i++,buf);
}

The above is part of my program.
I just only want to read the file argv[1] and write to the console,but the output tell me:
If the specified file is smaller than 2047bytes,It works fine.But if larger than 2047,the file then cannot written to the console completely.

So how should i do then?
Thanks for help.

Last edited by whepin; 04-08-2002 at 06:13 AM.
 
Old 04-08-2002, 10:20 PM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
One question for you:

Why do you have this line in your program?
Code:
buf[strlen(buf)-1]=0;
Figure out your problem yet?

Since what you read isn't a null terminated string, you are trying to NULL terminate it. The problem is you are using strlen on a non string... Grab the length of the string you read and tack the NULL on after that.


Code:
#include <stdio.h>
#include <stdlib.h>

#define BUFFER_SIZE 2047

main(int argc, char * argv[])
{
   char buf[BUFFER_SIZE + 1]; 
   int i=1; 
   FILE * fin;
   FILE * fout;
   int iRead = 0;

   fin = fopen( argv[1], "r" ); 
   fout = fdopen( 1, "w" ); 
   memset( buf, 0, sizeof( buf )); 
   while(( iRead = ( fread( buf, sizeof( char ), BUFFER_SIZE, fin))) > 0 ) 
   { 
      buf[ iRead ] = 0; 
      fprintf( fout,"ret times=%d, iRead = %d, [%s]\n", i++, iRead, buf); 
   } 

}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
memery used by fprintf() lordofring Programming 2 09-14-2005 10:43 AM
fprintf hangs at __lll_mutex_lock_wait ugtech Red Hat 1 04-20-2005 08:49 AM
my time is wrong and calender is also wrong Paxmaster Linux - General 6 12-16-2004 12:46 AM
What's wrong? Gero Programming 1 12-12-2004 11:45 AM
fprintf is not working in rpc server pgm ratheesh Programming 1 01-08-2004 08:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration