C语言 从文件中写入和读取并按升序对它们进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18596323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:21:14  来源:igfitidea点击:

Writing and reading from a file and sort them in ascending order

cfile-iofgetsscanfbubble-sort

提问by venkatappa jv

#include<stdio.h>
void sort(int *p, int size)
{
  int i, j;
  for (i = 0; i < size - 1; ++i)
  {
    for (j = 0; j < size - i - 1; ++j)
    {
      if (p[j] > p[j + 1])
      {
        int temp;
        temp = p[j];
        p[j] = p[j + 1];
        p[j + 1] = temp;
      }
    }
  }
}

void createtestfile()
{
  FILE *f1;
  f1 = fopen("program.txt", "w");
  fprintf(f1, "6#this is comment\n");
  fprintf(f1, "3#this is comment\n");
  fprintf(f1, "7#this is comment\n");
  fprintf(f1, "2\n");
}

void readtestfile()
{
  FILE *fp;
  char buff[1024];
  int value;

  int number_of_lines;
  fp = fopen("program.txt", "r");
  do
  {
    fgets(buff, 1024, fp);
    fscanf(fp, "%d", &value);
    number_of_lines++;
    buff[number_of_lines] = value;
  } while (fp != EOF);

  sort(buff, number_of_lines);

  int i;
  for (i = 1; i < number_of_lines; i++)
  {
    printf("value is %d", buff[i]);
  }
}

int main()
{
  createtestfile();
  readtestfile();
  return 0;
}

I am writing a string to a file. Later reading only integers from a file and sort them in ascending order. I am using fgets to reading a line by line from the file and I have problem in reading only integers from a file.

我正在将字符串写入文件。稍后仅从文件中读取整数并按升序对它们进行排序。我正在使用 fgets 从文件中逐行读取,但在仅从文件中读取整数时遇到问题。

回答by alk

You are missing to close the file after having written to it.

写入文件后,您无法关闭文件。

Due to this the content most propably will be written when the application end, because to the then will be clsoed implicitly.

因此,内容最有可能在应用程序结束时写入,因为到那时将被隐式关闭。

Add

添加

fclose(f1) 

after the last fprintf()in createtestfile().

在最后一次fprintf()之后createtestfile()



Secondly when reading from the file you should decide whether to use fgets()of fscanf()to read in the data.

其次从文件中读取数据时,你应该决定是否使用fgets()fscanf()数据读取。

Or you can switch from reading from the file directly using fscanf()to do a sscanf()from the "string" you read using fgets().

或者,您可以从直接从文件读取切换到使用从读取的“字符串”中fscanf()执行 a 。sscanf()fgets()

To do so replace

这样做替换

fscanf(fp, "%d", &value);

with

sscanf(buff, "%d", &value);


Thirdly it makes no sense to try to write what you scanned in from buffback to buff, at least because you are overwriting buffin the next round of the read-loop.

第三,尝试写入从buffback to扫描的内容是没有意义的buff,至少因为您正在buff下一轮读取循环中覆盖。

Also you pass buffto sort()which should make the compiler yell out a loud warning.

你也传递buffsort()which 应该让编译器大声警告。

Initialise the loop counter number_of_linesproperly to 0and use an integer array to store the values you scanned out of the file's content. This you then can pass into sort().

number_of_lines正确初始化循环计数器0并使用整数数组来存储您从文件内容中扫描出的值。然后您可以将其传入sort().

回答by David Ranieri

fgetsis consuming line, you have to use sscanfinstead of fscanfafter fgets

fgets正在消耗线路,您必须使用sscanf而不是fscanfafterfgets

  do
  {
    fgets(buff, 1024, fp);
    fscanf(fp, "%d", &value); /* here */

回答by Aakash Anuj

Just for your knowledge, you can sort your data by using the inbuilt sort function qsort.

仅就您的知识而言,您可以使用内置的排序函数 qsort 对数据进行排序。

Here is an example:

下面是一个例子:

#include <stdio.h>      /* printf */
#include <stdlib.h>     /* qsort */

int values[] = { 40, 10, 100, 90, 20, 25 };

int compare (const void * a, const void * b)
{
  return ( *(int*)a - *(int*)b );
}

int main ()
{
  int n;
  qsort (values, 6, sizeof(int), compare);
  for (n=0; n<6; n++)
     printf ("%d ",values[n]);
  return 0;
}