可以从文件中获取元素,升级它们并将输出复制到文件中的程序。 [英] Program which can take elements from a file, ascend them and copy the output in a file.

查看:52
本文介绍了可以从文件中获取元素,升级它们并将输出复制到文件中的程序。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该程序将调用input.txt文件中的值。只有数字。然后按升序对它们进行排序,并将输出复制到output.txt文件中。

我编写了程序但仍停留在排序上。

我在这里提交我的代码另外,你可以从中看到,并且可以很容易地帮助我。

这里我已经做了一个函数来对数字进行排序。



我尝试过:



The program is to call the value from "input.txt" file. There are only numbers. Then sort them in ascending order and copy the output in "output.txt" file.
I had written the program but stuck on sorting.
I am submitting my code here also so you can see from that and can easily help me.
And here I had already made a function to sort the numbers.

What I have tried:

#include<stdio.h>

void sort_func( int arr[], int size ){
}

int main(){
FILE *in = fopen("input.txt", "r"), *out = fopen("output.txt", "w+");

int intarr[100];
int no_of_int;

if(in != NULL){
for(no_of_int=0; !feof(in); no_of_int++)
fscanf(in, "%d", &intarr[no_of_int]);

sort_func(intarr, no_of_int);

for(int i=0; i<no_of_int; i++)
fprintf(out, "%d\n", intarr[i]);
printf("process done!");
}
else printf("File not found!");
}

推荐答案

您使用代码处于正确的轨道上,但您必须使用fclose填充文件。并检查输出文件的打开是否成功。



我会在文件中使用一些格式,例如用逗号或CR分隔。



您可以在Google上找到的排序功能。
You are on the right track with your code, but you must clode the files with fclose. And also check the success of the opening for the output file.

I would use some formatting in the files like separating with a comma or CR.

Sorting functions you will find with Google.


这篇关于可以从文件中获取元素,升级它们并将输出复制到文件中的程序。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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