[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Problem with fscanf

Posted on 2008-10-16
7
Medium Priority
?
497 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 2000 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

873 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