LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 09-06-2005, 02:01 PM   #1
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
weird fstream output


Code:
schneidz@lq> cat schneidz-09.03.2005.c
#include "stdio.h"
#include "string.h"

main(int argc, char *argv[])
{
        char col1[255], temp[255], s[] = "0"  ;
        int c;
        FILE * fstream;

        fstream = fopen(argv[1], "r+");

        c = fscanf(fstream, "%s", &col1);
        while(c == 1)
        {
         if(strcmp(col1,"55555") == 0)
         {
          fprintf(fstream, "hello-world");
         }
         c = fscanf(fstream, "%s", &col1);
        }
        fclose(fstream);
}
schneidz@lq> cat schneidz.txt
hello
world
l33t
h4x0r
55555
chun-li
akuma
schneidz@lq> schneidz.x schneidz.txt
schneidz@lq> cat schneidz.txt
hello
world
l33t
h4x0r
55555
chun-li
akuma
hello
world
l33t
h4x0r
55555hello-worlduschneidz@lq>
what i would want it to look like is:
Code:
schneidz@lq> cat schneidz.txt
hello
world
l33t
h4x0r
55555hello-world
chun-li
akuma
can someone help me figure out how to get the correct output from 'fprintf'.
(why is it printing everything up to the 'strcmp' twice?)
'man fprintf' isn't explaining what i am seeing.

thanks,
 
Old 09-06-2005, 02:15 PM   #2
perry
Member
 
Registered: Sep 2003
Location: USA & Canada
Distribution: Slackware 12.0
Posts: 978

Rep: Reputation: 30
one thing you might consider is using fputs instead of fprintf as by default, fprintf expects the 2nd parameter to be it's formatting string. if anything in that parameter looks like a control sequence (aka "%s" or "%x") it's essentially going to look for a 3rd parameter which you have not specified. however, being c/c++, it will look for a 3rd parameter irreguardless if that is what you meant or not, getting the info off the stack. it's a wonder your app didn't bomb with stack violations or anything else... there again, this is Linux so you are a bit safer than the world of winblows.

hope this helps

- perry
 
Old 09-06-2005, 02:42 PM   #3
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Original Poster
Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
thanks perry but the same thing happened (without the last charecter being 'u').

does anyone have any other ideas?


i added this:
Code:
fflush(fstream);
after the second fscanf.

and it yeilded this:
Code:
schneidz@lq> cat  schneidz.txt
hello
world
l33t
h4x0r
55555
chun-li
akuma

55555hello-worldschneidz@lq>
but it's still not edited in place.

Last edited by schneidz; 09-06-2005 at 02:55 PM.
 
Old 09-06-2005, 03:48 PM   #4
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
hehe looks like your trying to do a cat like program. I have done one before. You may
want to try outputing each charecter from the stream rather then obtain string information.

at least thats how i got mine to work.

Code:
 
#include "stdio.h"
#include "string.h"

main(int argc, char *argv[])
{
        char col1[255], temp[255], s[] = "0"  ;
        int c;
        FILE * fstream;

        fstream = fopen(argv[1], "r+");

        c = fscanf(fstream, "%s", &col1);
        while(c == 1)
        {
         if(strcmp(col1,"55555") == 0)
         {
          fprintf(fstream, "hello-world");
         }
         c = fscanf(fstream, "%s", &col1);
        }
        fclose(fstream);
}
also you have decalared a pointer here but your code does nothing with it

s[] = "0"

this one is also never used
temp[255]

Last edited by exvor; 09-06-2005 at 04:50 PM.
 
  


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
weird sed output schneidz Programming 3 05-19-2005 11:16 AM
Weird DF Output Mr_Munkey Linux - General 4 03-09-2005 09:11 AM
Weird Dmesg Output raid517 Linux - Hardware 2 09-22-2003 10:14 PM
C: Weird characters on output notsoevil Programming 3 06-23-2003 04:00 PM
weird nestat output tyler_durden Linux - Security 1 02-28-2002 12:31 PM

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

All times are GMT -5. The time now is 12:35 PM.

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