p14
p14

Reputation: 1

C project with files

I need some help with my C project: I need to write a c program who receives 2 parameters:

1) The name of a text file(infile) which is in the same catalog

2) A number k>0 And creates 2 new files,outfile1 & outfile 2 as:

Outfile 1: k,2*k,3*k…. character of infile

Outfile 2: k,2*k,3*k…..line of infile

Example:

INFILE

Abcdefg
123456
XXXXXX
01010101

OUTFILE 1:

Cf25XX101

OUTFILE 2:

XXXXXX

I wrote some code ,but its not working. Any ideas?

 #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;

}

My actual problem is calculate and write into files (outfile1 and outfile2) the result...

Upvotes: 0

Views: 109

Answers (1)

Zhigang An
Zhigang An

Reputation: 294

  1. You need to close file after finishing reading/writing it with fclose.

  2. You can create and write strings to a file using fopen with correct mode.

  3. You can output formatted string to a file by using fprintf.

  4. It seems that you don't want to print the 0th character/line, so in the last for loop, i should start from 1 (or start from 0 but add 1 later).

  5. array[c] is a string, not a character. So when printing it, you should use %s specifier instead of %c.

  6. It is not a good idea using char as count in later for loops unless you know input file will be very short. signed char can only count to 127 before overflow (unsigned char can count to 255). But if you have a very long file, for example thousands of lines, this program would not work properly.

  7. array is malloced in function char** read_lines(FILE* txt, int* count). After finish using it, you need to dealloc, or free it by calling

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

    and followed by free(array). This avoids memory leakage.

Modified code is here. In the following code, char c is not used. This is the part where you process output files, and before return 0; in main function.

    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);

Upvotes: 1

Related Questions