从TXT文件C阅读和比较数字 [英] Reading and comparing numbers from txt file C

查看:111
本文介绍了从TXT文件C阅读和比较数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的C语言编程,所以我有以下问题的困难。

I am new to C programming, so I am having difficulties with the problem below.

我有一个文本文件 inp.txt 包含类似以下信息:

I have a text file inp.txt which contains information like the following:

400;499;FIRST;
500;599;SECOND;
670;679;THIRD;

我需要输入一个号码,我的程序需要将其从 inp.txt 文件编号进行比较。

例如,如果我输入450,它是400和499之间,所以我需要写的字第一个到文件 out.txt

For example, if I type 450, it's between 400 and 499, so I need write to the word FIRST to the file out.txt

我不知道如何将一个字符数组转换为int。

I have no idea how to convert a character array to an int.

推荐答案

我想你会想在你的程序一般步骤(但我将让你找出你想怎么办呢准确)

I think you'll want these general steps in your program (but I'll leave it to you to figure out how you want to do it exactly)


  1. 从文件inp.txt装入各范围和文本的第一,第二等,为一个阵列,或几个阵列或相似的。正如我在评论说以上,的fscanf 可能会派上用场。本页面介绍了如何使用它 - 网页是关于C ++,但在C使用它应该是相同的 http://www.cplusplus.com/reference/clibrary/cstdio/fscanf/ 。粗略地说,这个想法是,你给的fscanf 的格式说明你想从一个文件中提取一行,以及它把它找到的位转换成变量你说明)

  2. 提示用户输入一个数字。

  3. 查找通过数组(县)工作,这不等数量适合,因此,它的文本输出

  1. Load each of the ranges and the text "FIRST", "SECOND", etc. from the file inp.txt, into an array, or several arrays, or similar. As I said in the comment above, fscanf might be handy. This page describes how to use it - the page is about C++, but using it in C should be the same http://www.cplusplus.com/reference/clibrary/cstdio/fscanf/. Roughly speaking, the idea is that you give fscanf a format specifier for what you want to extract from a line in a file, and it puts the bits it finds into the variables you specify)
  2. Prompt the user to enter a number.
  3. Look through the array(s) to work out which range the number fits into, and therefore which text to output

编辑:我会在一些细节,因为提问者提出要求。这仍然是一种骨骼的给你一些想法。
使用的fopen 的功能,这样的事情(声明指针 FILE * INPUT_FILE

I'll put some more detail in, as asker requested. This is still a kind of skeleton to give you some ideas. Use the fopen function, something like this (declare a pointer FILE* input_file):

input_file = fopen("c:\\test\\inp.txt", "r") /* "r" opens inp.txt for reading */

然后,这是很好的检查,如果通过检查INPUT_FILE == NULL该文件已成功打开,

然后使用的fscanf 从文件的一行读的细节。遍历文件的行,直到您已经阅读了整个事情。你给的fscanf 指针,你想让它从文件到的每一行把信息的变量。 (这有点像一个反向的printf 格式符)。

Then use fscanf to read details from one line of the file. Loop through the lines of the file until you've read the whole thing. You give fscanf pointers to the variables you want it to put the information from each line of the file into. (It's a bit like a printf formatting specifier in reverse).

所以,你可以声明 INT RANGE_START,RANGE_END 字符RANGE_NAME [20] 。 (为简单起见,我们假设所有的话都在最长20个字符,这可能不是在虽然长远计划好)。

So, you could declare int range_start, range_end, and char range_name[20]. (To make things simple, let's assume that all the words are at most 20 characters long. This might not be a good plan in the long-run though).

while (!feof(input_file)) { /* check for end-of-file */

   if(fscanf(input_file, "%d;%d;%s", &range_start, &range_end, range_name) != 3) {
      break; /* Something weird happened on this line, so let's give up */
   else {
      printf("I got the following numbers: %d, %d, %s\n", range_start, range_end, range_name);
   }
}

希望这给你的一些想法。我试着运行此code和它似乎工作。不过,值得一说的fscanf也有一些缺点(如见 http://mrx.net/c/readfunctions.html ),所以另一种方法是使用与fgets 来获取每一行(与fgets的好处是,你能指定的字符阅读的最大数量,所以有没有超越一个字符串缓冲区长度的危险),然后的sscanf 来从字符串到整型变量读取。我还没有尝试过这种方式,但。

Hopefully that gives you a few ideas. I've tried running this code and it did seem to work. However, worth saying that fscanf has some drawbacks (see e.g. http://mrx.net/c/readfunctions.html), so another approach is to use fgets to get each line (the advantage of fgets is that you get to specify a maximum number of characters to read, so there's no danger of overrunning a string buffer length) and then sscanf to read from the string into your integer variables. I haven't tried this way though.

这篇关于从TXT文件C阅读和比较数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆