What is the programming for rewriting text files to csv format?

Asked 2 years ago, Updated 2 years ago, 50 views

How to rewrite toshi.txt as the corresponding table toshi.csv.

|TOKYO|155|1|TOKYO,155,1,
|NAGOYA | 108 | 3 | → NAGOYA, 108, 3,

Please tell me the programming for rewriting like this.
I understand that you can rewrite "|" to "," but it doesn't work.
I am a complete beginner in programming, so please take good care of me.

#define_CRT_SECURE_NO_WARNINGS
# include <stdio.h>

int main()
{
    char line [100]; // For storing rows read from a file
    FILE* in_file, *out_file; // For storing file pointers
    inti;

    printf("Change a text file toshi.txt to csv file.\n";

    in_file=fopen("toshi.txt", "r"); 
    if(in_file==NULL){
        printf("fopen_in error");
        return 0;
    }
    out_file=fopen("toshi.csv", "w"); 
    if(out_file==NULL){
        printf("fopen_outerror");
        return 0;
    }

    while(fgets(line,100,in_file)!=NULL){
        i = 1;
        while(line[i]!='\n'){
            switch(line[i]){ 
            case '|':
                line[i] = ', ';
                break;
                if(line[i]!='|') {line[i]=line[i];}
            }

            i++;
        }
        fputc(line[i], out_file);
    }

    fclose(in_file);
    fclose(out_file);
    return 0;
}

c csv

2022-09-30 20:32

3 Answers

understanding the break in the switch-case statement:

 case '|':
    line[i] = ', ';
    break;
    if(line[i]!='|') {line[i]=line[i];}

The break in the case means "get out of this switch statement".
In other words, the if statement after the break statement is never executed.
A statement that never runs like this is sometimes called dead code.

determining if the sentence ends:

while(line[i]!='\n'){

This action is to loop to the end of the sentence (line feed position), but if there is no \n after the last line of toshi.txt, the loop may not stop.

 // Cases where the last line has a new line
| TOKYO | 155 | 1 | ↓
|NAGOYA|108|3|↓
[EOF]

// If there is no new line in the last line
| TOKYO | 155 | 1 | ↓
|NAGOYA|108|3|[EOF]

[ EOF—end of file, end of file

Also check if it is a NULL character ('\0') that represents a string termination as follows:

// Loops while the character is not a new line ('\n') or a NULL character ('\0')
while(line[i]!='\n'&line[i]!='\0'){

'|' to '',' replacement action:

Consider not adding ', ' (comma) to the CSV termination after conversion.

|TOKYO|155|1|→TOKYO,155,1

The | at the beginning of the line can be ignored by starting the loop from 1.
The | at the end of the line should not print anything when the following characters are \n or \0.
(Replace the following characters (line[i+1]) with commas other than \n or \0)

while(fgets(line,sizeofline,in_file)!=NULL){
    i = 1;

    // Loops while the character is not a new line ('\n') or a NULL character ('\0')
    while(line[i]!='\n'&line[i]!='\0'){
        switch(line[i]){
        case '|':
            // When '|', the next character (i+1) is not a new line ('\n') or a NULL character ('\0')
            // Print ', ' to replace '|'
            if(line[i+1]!='\n'&line[i+1]!='\0'){
                fputc(',',',out_file);
            }
            break;
        default:
            // Print characters other than '|' as they are
            fputc(line[i], out_file);
            break;
        }

        i++;
    }

    // New line after replacing '|'→', ' to the next line
    fputc('\n', out_file);
}

understanding the sizeof operator:

To put it roughly, the size of operator returns the size of the array when you pass it.
(Returns 100 of line[100] in this case.)
This allows you to modify only one source code when you need to change the size from 100.

Please study the size of operator separately.

Summarize similar actions into functions:

By the way, regarding the code I mentioned earlier, there are two processes that determine whether it is the end of the sentence ○!='\n'&○!='\0'.These may be summarized into a function called isEndOfLine(○).

...
    while(fgets(line,sizeofline,in_file)!=NULL){
        i = 1;

        // Loops while characters are not at the end of a sentence
        while(isEndOfLine(line[i])==0){
            switch(line[i]){
            case '|':
                // When '|', the following characters (i+1) are not at the end of the sentence:
                // Print ', ' to replace '|'
                if(isEndOfLine(line[i+1])==0){
                    fputc(',',',out_file);
                }
                break;
            default:
                // Print characters other than '|' as they are
                fputc(line[i], out_file);
                break;
            }

            i++;
        }

        // New line after replacing '|'→', ' to the next line
        fputc('\n', out_file);
    }

    fclose(in_file);
    fclose(out_file);
    return 0;
}

/**
 * determine whether a character is at the end of a sentence
 * @paramc characters
 * Returns 1 if the @return character is a new line ('\n') or a NULL character ('\0')Otherwise return 0
 */
intisEndOfLine(charc){
    if(c=='\n'||c=='\0'){
        return1;
    }
    return 0;
}


2022-09-30 20:32

If you change it to the following, I think the behavior is generally expected by the questioner.
(Although it is not strict as a CSV)

while(fgets(line,sizeofline,in_file)!=NULL){
    for(i=1;line[i]!='\n';++i) {//for.There must be a new line at the end of the line.
        If(line[i]=='|')// There is only one condition, so I changed it to if.
            line[i] = ', ';
        fputc(line[i], out_file);
    }
    fputc('\n', out_file);// You must print a new line at the end of the line.
}


2022-09-30 20:32

How about converting with sed?
It's not programming, but

$sed-e's/|/, /g'toshi.txt>toshi.csv


2022-09-30 20:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.