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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
09-06-2005, 02:01 PM
|
#1
|
|
Senior Member
Registered: May 2005
Location: boston, usa
Distribution: fc-12/ fc-11-live-usb/ aix
Posts: 2,732
|
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,
|
|
|
|
09-06-2005, 02:15 PM
|
#2
|
|
Member
Registered: Sep 2003
Location: USA & Canada
Distribution: Slackware 12.0
Posts: 978
Rep:
|
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
|
|
|
|
09-06-2005, 02:42 PM
|
#3
|
|
Senior Member
Registered: May 2005
Location: boston, usa
Distribution: fc-12/ fc-11-live-usb/ aix
Posts: 2,732
Original Poster
|
thanks perry but the same thing happened (without the last charecter being 'u').
does anyone have any other ideas?
i added this:
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.
|
|
|
|
09-06-2005, 03:48 PM
|
#4
|
|
Senior Member
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: LFS-Version SVN-20091202, Arch 2009.08
Posts: 1,467
Rep:
|
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 06:20 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|