排序C文件

扫码查看
本文介绍了排序C文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

右键于是我就问here我怎么可以排序我的C文件,这是我创造了codeD回应。

Right so I asked here how I can sort my c file, and this was the coded response I created.

 #include <stdio.h>
 #include <conio.h>

 int main()
 {
     FILE *fN;
     FILE *fS;

     fN=fopen("Numbers.txt","r");
     fS=fopen("Sorted.txt","w");
     system("sort Numbers.txt > Sorted.txt");
     getch();
     fclose(fS);
     fclose(fN);
 }

这总是想出了同样的错误信息:因为它正在被其他进程使用进程无法访问文件

This always comes up with the same error message: The process cannot access the file because it is being used by another process.

这是不是意味着我将展示改变库?我真的很困惑,为什么有错误。

does this mean I show change the libraries?, I'm really confused to why there is an error.

推荐答案

这是你在上面根本张贴code使用排序命令行实用程序进行排序。当你写

The code that you're posting above fundamentally uses the sort command-line utility to do the sorting. When you write

system("sort Numbers.txt > Sorted.txt");

你调用排序 Numbers.txt 文件,然后将命令的输出重定向到文件 Sorted.txt

you're invoking sort on the Numbers.txt file, then redirecting the output of the command to the file Sorted.txt.

与code中的问题是,你尝试这样做之前,你写

The problem with your code is that before you try to do this, you're writing

fS=fopen("Sorted.txt","w");

这将打开 Sorted.txt 写作,这在大多数操作系统将被任何其他进程写入锁定文件 - 包括你的排序过程。为了解决这个问题,只是消除所有的的fopen FCLOSE 电话和只写

This opens Sorted.txt for writing, which on most operating systems will lock the file from writing by any other process - including your sort process. To fix this, just eliminate all the fopen and fclose calls and just write

int main() {
    system("sort Numbers.txt > Sorted.txt");
}

在公平,如果这就是你的程序做,只需在命令行中执行上面的命令。如果你正在做这个作为一个子程序,但是,只要使用系统命令,不要做任何手动的fopen FCLOSE 通话。

In fairness, if this is all that your program does, just execute the above command from the command line. If you're doing this as a subroutine, though, just use the system command and don't do any manual fopen or fclose calls.

希望这有助于!

这篇关于排序C文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:24
查看更多