【问题标题】:C project with files带文件的 C 项目
【发布时间】:2017-04-08 19:45:45
【问题描述】:

我的 C 项目需要一些帮助: 我需要编写一个接收2个参数的c程序:

1) 同一目录下的文本文件(infile)的名称

2) 一个数字 k>0 并创建 2 个新文件,outfile1 & outfile 2 为:

输出文件 1:k,2*k,3*k...。 infile 的字符

输出文件 2:k,2*k,3*k…..infile 行

例子:

文件

Abcdefg
123456
XXXXXX
01010101

文件 1:

Cf25XX101

文件 2:

XXXXXX

我写了一些代码,但它不起作用。有什么想法吗?

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


char** read_lines(FILE* txt, int* count) {
    char** array = NULL;        
    int    i;                   
    char   line[100];           
    int    line_count;          
    int    line_length;         
    *count = 0;                 
    line_count = 0;              
    while (fgets(line, sizeof(line), txt) != NULL) {                                      
       line_count++;
    }
    rewind(txt);                
    array = malloc(line_count * sizeof(char *));    
    if (array == NULL) {
        return NULL;                                
    }
     for (i = 0; i < line_count; i++) {             
        fgets(line, sizeof(line), txt);             
        line_length = strlen(line);                 
        line[line_length - 1] = '\0';
        line_length--;                              
        array[i] = malloc(line_length + 1);         
        strcpy(array[i], line);                    
    }
    *count = line_count;                           
    return array;                                  
}

int main(int argc, char * argv[]) {
    char**      array    = NULL;    
    FILE*       file     = NULL;    
    const char* filename = NULL;    
    int         i;                  
    int         line_count;         
    int         k;                  
    char        c;                  
    printf("ENTER ONE PHYSICAL NUMBER\n");         
    do{                                            
        if(k>0)
            scanf("%d",&k);

        else{
            printf("ENTER ONE PHYSICAL NUMBER\n");
            scanf("%d",&k);
        }
    }while(k<=0);
    file = fopen("LEIT.txt", "rt"); 
    if (file == NULL) {
        printf("CANT OPEN FILE %s.\n", filename);
        return 1;
    }
     array = read_lines(file, &line_count);   
     printf("ARRAY:\n");
     for (i = 0; i < line_count; i++) {       
        printf("[%d]: %s\n", (i+1), array[i]);
    }
    printf("CALCULATING OUTFILE1 AND OUTFILE2\n");
    printf("OUTFILE1:\n");
    for(i=0;i<line_count;i++){              
        c=i*k;                              
        printf("%c\n",array[c]);              
    }
    printf("WRITING OUTFILE1 COMPLETE!\n");

    printf("OUTFILE2:\n");
    for(i=0;i<line_count;i++){              
        c=i*k;                                
        printf("%c\n",array[c]);             
    }
    printf("WRITING OUTFILE2 COMPLETE!\n"); 

    return 0;

}

我的实际问题是计算并将结果写入文件(outfile1 和 outfile2)...

【问题讨论】:

    标签: c file input file-io output


    【解决方案1】:
    1. 使用fclose完成读/写后需要关闭文件。

    2. 您可以使用fopen 以正确的模式创建字符串并将其写入文件。

    3. 您可以使用fprintf将格式化字符串输出到文件。

    4. 看来你不想打印第0个字符/行,所以在最后一个for循环中,i应该从1开始(或者从0开始,后面加1)。

    5. array[c] 是一个字符串,而不是一个字符。所以在打印的时候,你应该使用%s说明符而不是%c

    6. 在以后的for 循环中使用char 作为计数不是一个好主意,除非您知道输入文件会很短。 signed char 在溢出前只能计数到 127(unsigned char 可以计数到 255)。但是如果你有一个很长的文件,比如几千行,这个程序就不能正常工作。

    7. array 分配在函数char** read_lines(FILE* txt, int* count) 中。用完后需要dealloc,或者调用释放

      for (i = 0; i < line_count; i++) {
          free(array[i]);
      }
      

      然后是free(array)。这样可以避免内存泄漏。

    修改后的代码在这里。在下面的代码中,char c 没有被使用。这是您处理输出文件的部分,在主函数中 return 0; 之前。

        printf("CALCULATING OUTFILE1 AND OUTFILE2\n");
        printf("OUTFILE1:\n");
    
        // Since we finished using LEIT.txt, close it here.
        fclose(file);
    
        // Mode: "w" - Write file. "+" - Create if not exist.
        // You can lso use "a+" (append file) here if previous record need to be preserved.
        FILE *out1 = fopen("OUTFILE1.txt", "w+");
        FILE *out2 = fopen("OUTFILE2.txt", "w+");
    
        if ((out1 == NULL) || (out2 == NULL)) {
            printf("CANT CREATE OUTPUT FILES.\n");
            return 1;
        }
    
        // Out file 1.
        unsigned int count = k;
    
        for (i = 0; i < line_count; i++){
            while (count < strlen(array[i])) {
                // This just prints to stdout, but is good for debug.
                printf("%c", array[i][count]);
    
                // Write to the file.
                fprintf(out1, "%c", array[i][count]);
    
                // Calculate c for next char.
                count += k + 1;
            }
    
            // Before go to next line, minus string length of current line.
            count -= strlen(array[i]);
        }
        printf("\n");
        printf("WRITING OUTFILE1 COMPLETE!\n");
    
        // Close file.
        fclose(out1);
    
        // Out file 2.
        printf("OUTFILE2:\n");
    
        for (i = 1;i < line_count / k; i++){
            count = i * k;
    
            // This just prints to stdout, but is good for debug.
            printf("%s\n", array[count]);
    
            // Write to the file.
            fprintf(out2, "%s\n", array[count]);
        }
        printf("WRITING OUTFILE2 COMPLETE!\n");
    
        //Close file.
        fclose(out2);
    
        // dealloc malloced memory.
        for (i = 0; i < line_count; i++) {
            free(array[i]);
        }
        free(array);
    

    【讨论】:

      猜你喜欢
      • 2015-12-11
      • 2019-10-13
      • 2012-08-04
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多