Solved

Problem with fscanf

Posted on 2008-10-16
7
479 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
[X]
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
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
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

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 opening and reading files 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