Solved

Problem with fscanf

Posted on 2008-10-16
7
469 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

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…
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…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files 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.

786 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