c - 每当我编译程序时,我都无法运行 gcc 为我创建的文件

标签 c file gcc

使用 gcc 下面的代码创建一个不可执行的文件。另外,当我包含 sort.h 头文件时,我收到错误。感谢任何愿意校对我的代码的人。

该程序的预期功能是打开提供的文件,然后对其进行排序。

file_sorter.c:

#include <stdio.h>
#include <stdlib.h>
//#include <sort.h>
#include <string.h>

int main(int argc, char **argv){
    if(argc != 2) {
        printf("usage: file_sorter <filename>\n");
        exit (EXIT_FAILURE);
    }
    char *out = "sorted.txt"; //declares the name of output file

    FILE *fp, *fileOUT; //Pointer to sort output data

    fp = fopen(*argv, "r");

    if(fp = NULL){ //checks that the file opened
        exit (EXIT_FAILURE);
    }

    char *line = NULL; //Grabs the total amount of lines from the file
    size_t len = 0;
    int totalLines = getline(&line, &len, fp);

    char **array = (char**)malloc(totalLines * sizeof(char*));
    char singleline[totalLines]; //initial memory allocation for the file

    int i = 0;
    while(fgets(singleline, totalLines, fp) != NULL){
        array[i] = (char*) malloc (totalLines * sizeof(char));
        singleline[totalLines] = '\0';
        strcpy(array[i], singleline);
        i++;
    }//loading the file, and allocating memory as we go

    printf("unsorted file:\n");//prints the unsorted array
    for(i=0; i<totalLines; i++){
        printf("%s\n", array[i]);
    }

    fileOUT = fopen(out, "w");//opens the out file
    if(!fileOUT){
        exit (EXIT_FAILURE);
    }

    printf("sorted file:\n");//prints out the sorted file
    for(i=0; i<totalLines; i++){
        fprintf(fileOUT, "%s", array[i]);
    }

    fclose(fp);//close the files
    fclose(fileOUT);

    for(i=0; i<totalLines; i++){//anti memory leak
        free(array[i]);
    }
    free(array);

    return 0;
}

排序.c:

#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>

void quick(int *a, int n){
    if(n < 2){
        return;
    }
    int p = a[n /2];
    int *l = a;
    int *r = a + n -1;
    while( l <= r ){
        if( *l < p ){
            l++;
        }
        else if ( *r > p ){
            r--;
        }
        else {
            int t = *l;
            *l = *r;
            *r = t;
            l++;
            r--;
        }
    }
    quick( a, r - a + 1 );
    quick( l, a + n - l );
}

void insertion(int *a, const size_t n) {
    size_t i, j;
    int value;
    for (i = 1; i < n; i++) {
        value = a[i];
        for (j = i; j > 0 && value < a[j - 1]; j--) {
            a[j] = a[j - 1];
        }
        a[j] = value;
    }
}

排序.h:

#ifndef SORT_H
#define SORT_H
#include <stddef.h>

void quick(int *a, int n);
void insertion(int *a, const size_t n);
void readFile(char *a, char *b)

#endif /*SORT_H*/

生成文件:

all: file_sorter.o sort.o
    gcc -Wall -o file_sorter file_sorter.o sort.o

file_sorter.o: file_sorter.c sort.h
    gcc -c file_sorter.c

sort.o: sort.c sort.h
    gcc -c sort.c

clean:
    rm -rf sort *.o

最佳答案

您没有打开任何内容(除了尝试打开调用程序名称):

fp = fopen(*argv, "r");

回想一下*argv,即argv[0]是调用程序名称(即“file_sorter”)。您应该使用 argv[1] 来作为第一个命令行参数。即:

fp = fopen(argv[1], "r");

打开命令行上提供的文件名。修复该问题,然后我们将处理其他问题。

关于c - 每当我编译程序时,我都无法运行 gcc 为我创建的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25561740/

相关文章:

javascript - 页面完成后加载文件而不重定向

iOS:防止 .cacheDirectory 中的文件在存储空间已满警告时被删除

c++ - 用于使用 g++ 编译的 .vimrc 脚本

c - 如何系统地遵循递归?

C-SERVER - 在 : strncpy(ii2, iii + 5, 3) 的奇怪行为;

python - 在运行时将文件中的每个段落读取到多个列表中

gcc - 如何在GCC中的32字节边界处对齐堆栈?

c - cpp 和 gcc -E 的区别

c - 将二维数组传递给递归

c - GTK 设置默认打印机