Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Problem with fscanf

Posted on 2008-10-16
Medium Priority
Last Modified: 2012-05-05

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?

Question by:scuzz1
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
LVL 53

Accepted Solution

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 ...
LVL 45

Expert Comment

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");

Author Closing Comment

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,

Industry Leaders: 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

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 ?

Author Comment

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

Expert Comment

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.

Author Comment

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.


Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-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.

610 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