LinuxQuestions.org
Help answer threads with 0 replies.
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 12-11-2004, 01:52 AM   #1
bostonbanana
LQ Newbie
 
Registered: Dec 2004
Posts: 3

Rep: Reputation: 0
reading in string with fgets


Hello. I'm a newbie here and this is my first post.

I am having so much trouble with this...i am about to die!!!
I am trying to read in from a file that has:

first, second, third line contain some random string with white spaces
rest of the lines contain int or float...

this is part of the code i wrote:
Code:
while ((fgets(temp1.Name, 50, ifp) && 
          fgets(temp1.Type, 50, ifp) &&
	  fgets(temp1.Size, 50, ifp) != NULL) &&
          (fscanf (ifp, "%d%d%f%d%f%d%d%d%d",
                 &temp1.c, &temp1.s,
                 &temp1.f, &temp1.c,
                 &temp1.s, &temp.1c, 
                 &temp1.g, &temp1.f,
		   &temp1.p) != EOF))
     {
        if((x = strstr(temp1.Name, "\n"))!= NULL) *x=0;
        if((x = strstr(temp1.Type, "\n"))!= NULL) *x=0;
        if((x = strstr(temp1.Size, "\n"))!= NULL) *x=0;

   
        temp = CreateNode();
        SetData(temp, temp1);
        Insert(&headNode, temp);
     }
Please help!!!
 
Old 12-11-2004, 04:50 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Where is the problem exactly? I must say that I'd do this in a completely different way. I'd read it char after char, checking which line it is in. Also, using fscanf is not a very good idea if there's possibility of values not beeing fully correct.
 
Old 12-11-2004, 05:49 PM   #3
bostonbanana
LQ Newbie
 
Registered: Dec 2004
Posts: 3

Original Poster
Rep: Reputation: 0
there is total of 12 items i am reading in...and those 12 items repeat...
after reading in the first 12 items, it messes up...

anyways, the code works fine it's just that i didn't not expect a new line character after 12th item(integer) would cause a problem since i am reading it in with fscanf.
so after fixing it to

[code]while ((fgets(temp1.Name, 50, ifp) &&
fgets(temp1.Type, 50, ifp) &&
fgets(temp1.Size, 50, ifp) != NULL) &&
(fscanf (ifp, "%d%d%f%d%f%d%d%d%d%c",
&temp1.c, &temp1.s,
&temp1.f, &temp1.c,
&temp1.s, &temp.1c,
&temp1.g, &temp1.f,
&temp1.p, &junk) != EOF))
[\code]

notice extra %c and i am reading it into junk.
it works fine now.
it only took me 12hrs to figure it out. T_T i am going to cry now...

thanks for responding
 
Old 12-11-2004, 06:14 PM   #4
bostonbanana
LQ Newbie
 
Registered: Dec 2004
Posts: 3

Original Poster
Rep: Reputation: 0
nevermind...
it doesn't quite work.
anyways,
if((x = strstr(temp1.Name, "\n"))!= NULL) *x=0;
if((x = strstr(temp1.Type, "\n"))!= NULL) *x=0;
if((x = strstr(temp1.Size, "\n"))!= NULL) *x=0;

is this sufficient to get rid of newline character at the end of a line?
man, this is frustrating!!!!
 
Old 12-12-2004, 02:10 PM   #5
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
It should work (the part with replacing '\n's).

Your problems may be related to the fact of 50 character limit and that fgets finishes after finding \n. Also using fscanf may not always be safe as it requires interpretation of the string which may be not always correct.
 
  


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
reading a char string of variable size in C introuble Programming 3 05-08-2005 01:07 PM
fgets never stop reading from socket!? Thinking Programming 1 04-06-2005 09:38 AM
fgets() problems in C AMMullan Programming 4 03-12-2004 04:39 AM
fgets h/w Programming 23 12-21-2003 06:20 PM
fgets vs gets cxel91a Programming 2 12-01-2003 12:36 PM

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

All times are GMT -5. The time now is 05:07 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