带有文件的C项目

时间:2016-11-24 13:20:42

标签: c file input file-io output

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

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

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

Outfile 1:k,2 * k,3 * k .... infile的性格

Outfile 2:k,2 * k,3 * k ...... .. infile

示例:

INFILE

Abcdefg
123456
XXXXXX
01010101

OUTFILE 1:

Cf25XX101

OUTFILE 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)结果......

1 个答案:

答案 0 :(得分:1)

  1. 您需要在使用fclose完成阅读/写作后关闭文件。

  2. 您可以使用fopen以正确的模式为文件创建和编写字符串。

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

  4. 您似乎不想打印第0个字符/行,因此在上一个for循环中,i应该从1开始(或从0开始但是稍后加1)。

  5. array[c]是一个字符串,而不是一个字符。因此,在打印时,您应该使用%s说明符而不是%c

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

  7. array在函数char** read_lines(FILE* txt, int* count)中被malloced。完成使用后,您需要dealloc,或通过调用

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

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

  8. 修改后的代码在这里。在以下代码中,未使用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);