LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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, 03:01 PM   #1
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,144

Rep: Reputation: 638Reputation: 638Reputation: 638Reputation: 638Reputation: 638Reputation: 638
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, 03: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, 03:42 PM   #3
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,144

Original Poster
Rep: Reputation: 638Reputation: 638Reputation: 638Reputation: 638Reputation: 638Reputation: 638
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 03:55 PM.
 
Old 09-06-2005, 04:48 PM   #4
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: LFS-Version SVN-20091202, Arch 2009.08
Posts: 1,496

Rep: Reputation: 68
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 05:50 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


All times are GMT -5. The time now is 01:45 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration