K
K
Konstantin Malyarov2016-04-24 14:05:26
C++ / C#
Konstantin Malyarov, 2016-04-24 14:05:26

Why, when reading a file, does it not write values ​​after processing?

There is code that reads incoming values.

#include <stdio.h>

int main(){
    char city[80];
    char index[80];
    int started = 0;
    
    puts("data=[");
    while(scanf("%79,%79[^\n]", city, index)==2){
        if(started){
            printf(",\n");
        }
        else{
            started = 1;
        }
        printf("{CITY = %s   INDEX = %s}", city, index);
    }
    printf("\n]");
    return 0;
}

There is a file with values ​​that need to be converted to another format.
Moscow,RU
London,EN
Minsk,BY

After the command in the console.
test < city.txt > outfile.txt
I get a file without values:
data=[

]

What could be the reason?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
diskconnect, 2016-04-24
@Konstantin18ko

Try the following expression in scanf:
scanf("%79[^,],%79[^\n]\n", city, index)
the first [^,] means to read until a comma is encountered, the second comma is to skip the comma character . A similar situation with \n.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question