Solved

Problem with fscanf

Posted on 2008-10-16
7
467 Views
Last Modified: 2012-05-05
Hi,

I am having a problem with fscanf. The program is reading in small text files and altering the data slightly and writing out to another file. It works good, until it hits a file the has tabs in the first line. It reads up to the tab as line1 and then after the tab as line2. My question is how can I get fscanf to read the entire line1 as line1. Does fscanf have a problem with tabs?

Jim
0
Comment
Question by:scuzz1
  • 3
  • 3
7 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 22730808
>> Does fscanf have a problem with tabs?

fscanf splits the data on whitespace by default, yes.

Instead of %s for example, you could use %[^\n] to read everything up to the newline.


But I would really suggest to use fgets to read a line instead ...
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 22730816
fscanf (fileptr, "%d\t%d", &int1, &int2);

format string should match the input.

You would be better off reading in a string and parsing it

while (fgets(buffer, MAX_LINE_LEN, file_ptr))
{
      char * temp = strtok (buffer, "\t"); //tab delimited file
      while (temp)
      {
           //process value pointed to by temp .. use strtol, strtod etc as suitable
           temp = strtok (NULL, "\t");
      }
}
0
 

Author Closing Comment

by:scuzz1
ID: 31506730
I thought that was the way to go. I just did not want to redo what I had. I was hoping that there was something that I could do that would let me keep fscanf in the program. This is a small one time do the deed and trash executable so I was looking for quick and easy. The %[^\n] option you wrote did not work so I guess fgets is the answer.

Thanks for your help,

Jim
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 53

Expert Comment

by:Infinity08
ID: 22734148
>> The %[^\n] option you wrote did not work so I guess fgets is the answer.

It should if used correctly ;) Can you show how you used it ?
0
 

Author Comment

by:scuzz1
ID: 22734193
fscanf (stream, "%[^\n]", char_array);
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22734339
>> fscanf (stream, "%[^\n]", char_array);

That's ok. I assume your problem is that the next fscanf does not behave correctly ? If so, then that's because the \n is still on the stream and is read first by the next fscanf.
So :

        fscanf (stream, "%[^\n]\n", char_array);

would skip that \n.

But again : fgets is a much better alternative.
0
 

Author Comment

by:scuzz1
ID: 22734388
I got a bunch of null chars in the arrays when I tried that fscanf thing. Like I said, I was just hoping. It would have worked with fscanf if it were just a few files that were messed up. It seems as though there are more than I thought. I will just go with the fgets.

Thanks Again.

Jim
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now