开发者

How do I accept only the numeric values from a file in C?

I have taken up a project and I would like some help. Basically it is a program to check whether some pins are connected or not on a board. (Well, that's the simplified version. The whole thing is a circuit with a microcontroller.)

The problem is that, when a pin is connected I get a numeric value, and when it's not connected, I get no value, as in it's a blank in my table.

How can I accept these values?

I need to accept even the blank, to know that its not connected, plus the table contains some other non-numeric values as well. I tried reading the file using the fscanf() function but it didn't quite work. I'm aware of only fscanf(), fread(), fgets() and fgetc() functions to read from different kinds of files.

Also, is it possible to read data from an Excel file using C?

An example of the table is:

FROM          TO
1             39
2   

Over here, the numbers 1 and 2 are under the column FROM and it tells which pin the first end of the connector is connected to. The numbers under TO tell us which pin the other end of the connector is connected to, and when the column is blank, it's not connected at one end.

Now what I'm trying to do is create a program to create an assembly language program for the micro controller, so I need to be able to read whether the connector is connected, and if it is the开发者_高级运维n to which pin? And accordingly, I need to perform some operations. (Which I can manage by myself).

The difficulty I'm facing is reading from a specific line and reading the blank.


Read the lines using fgets() or a relative. Then use sscanf() on the line, checking to see whether there were one or two successful conversions (the return value). If there's one conversion, the second value was empty or missing; if two, then you have both numbers safely.

Note that fscanf() and relatives will read past newlines unless you're careful, so they do not provide the information you need.


so your file is more like this

Col1  col2 \n
r1val1  r1val2\n 
.
.

and so on,if this is the case then use fscanf() to read the string (until \n)from the file.Then use strtok() function to break the string into tokens ,here is the tutorial of the same

http://www.gnu.org/s/hello/manual/libc/Finding-Tokens-in-a-String.html

hope this helps... one more humble suggestion..just work on c programming first if you are a newbie,don't directly go for microcontrollers,as there are lots of things that you might understand in a wrong way if you dont know some of the basic concepts...


This is a common problem in C. When line boundaries carry meaning in the grammar, it's difficult to directly read the file using only the scanf()-family functions.

Just read each line with fgets(3) and then run sscanf() on one line at a time. By doing this you won't incorrectly jump ahead to read the next line's first column.


Since there are two values on a line you can parse the first, find the next whitespace, then parse the next looking for it's absence as well. I say parse rather than scanf() as when I really want control, or have a huge volume of numbers to scan, I use calls in the strtol() family.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜