简体   繁体   English

如何只接受C语言文件中的数值?

[英]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. 我尝试使用fscanf()函数读取文件,但效果fscanf() I'm aware of only fscanf() , fread() , fgets() and fgetc() functions to read from different kinds of files. 我知道只有fscanf()fread()fgets()fgetc()函数可以读取不同类型的文件。

Also, is it possible to read data from an Excel file using C? 另外,是否可以使用C从Excel文件读取数据?

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. 在此处,数字1和2在“ FROM”列下,它指示连接器的第一端连接到哪个引脚。 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. TO下方的数字告诉我们连接器另一端连接到哪个引脚,并且当该列为空白时,一端没有连接。

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 then 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. 使用fgets()或亲戚阅读这些行。 Then use sscanf() on the line, checking to see whether there were one or two successful conversions (the return value). 然后在该行上使用sscanf() ,检查是否有一次或两次成功的转换(返回值)。 If there's one conversion, the second value was empty or missing; 如果进行一次转换,则第二个值为空或丢失;否则为0。 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. 请注意,除非您小心,否则fscanf()和亲戚将阅读过去的换行符,因此它们不会提供所需的信息。

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. 我说的是解析,而不是scanf(),因为当我真正想要控制时,或者要扫描大量数字时,我使用strtol()系列中的调用。

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 依此类推,如果是这种情况,则使用fscanf()从文件中读取字符串(直到\\ n)。然后使用strtok()函数将字符串分成令牌,这是相同的教程

http://www.gnu.org/s/hello/manual/libc/Finding-Tokens-in-a-String.html 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... 希望这对您有帮助。另一个卑微的建议。如果您是新手,请先进行C编程,不要直接使用微控制器,因为如果您不知道,可能会以错误的方式理解很多事情一些基本概念...

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. 这是C语言中的常见问题。当行边界在语法中带有含义时,仅使用scanf()- family函数很难直接读取文件。

Just read each line with fgets(3) and then run sscanf() on one line at a time. 只需使用fgets(3)读取每一行,然后一次在一行上运行sscanf() By doing this you won't incorrectly jump ahead to read the next line's first column. 这样,您不会错误地跳到阅读下一行的第一列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM