1
I have a file in this following format:
https://mega.nz/#! Dwxxrizk (link to download read file);
[105][32][55][10][32][32][32][32][32][32][32][32][32][32][32][32][32][32][32][32][32][82][101][108][101][97][115][101]...
I need to read the data in this format:
105 32 55 10 32 32 32 32 32...[32][32][32][32][32][32][32][32][32][32][32][32][82][101][108][101][97][115][101]...
I’m using the code snippet:
while( c != EOF) {
fscanf(ler, "%c", &c);
fscanf(ler, "%d", &num);
fscanf(ler, "%c", &a);
fprintf(traduzido, "%d ", num);
}
but the looping never ends, I’m also using this:
while(fscanf(ler, "%c", &c) && fscanf(ler, "%d", &num) && fscanf(ler, "%c", &a) != EOF) {
fprintf(traduzido, "%d ", num);
}
but he won’t stop reading the file, it always goes from two to three lines.
What is the error and why of the error? How can I read this file and leave it in the way I want, if there is an easier way?
Look at the code in full:
int main(void)
int num;
char c;
FILE *ler;
FILE *traduzido;
ler = fopen("readme.code.txt", "r");
traduzido = fopen("traduzido.txt", "w");
if(ler == NULL || traduzido == NULL) {
printf("Erro Na aberura do Arquivo");
}
while(1) {
if (fscanf(ler, "%c", &c) <= 0){
break;
}
fscanf(ler, "%d", &num);
fscanf(ler, "%c", &c);
fprintf(traduzido,"%d ", num);
}
while(fscanf(traduzido, "%d", &num) != EOF) {
printf("%c", num);
}
system("pause");
The correct is to check the output of the
scanf
. I’ve answered a similar question to that one, but obviously it’s lost in the sea of answers.– Jefferson Quesado
In this answer I talk a little about the return of
scanf
: https://answall.com/a/253415/64969; I believe it will help– Jefferson Quesado
By the way, this flow could be treated as the flowchart of this question: https://answall.com/a/251865/64969; "a": leitura, "b": if the end of the file happened, the loop ends
– Jefferson Quesado
I took a look, but with you, I read to the last item of the file over there, it’s in a looping
– Fernando
while( fscanf(ler,"%c", &c) != EOF ){
 
 fscanf(ler, "%d", &num);
 fscanf(ler, "%c", &c);
 fprintf(traduzido, "%d ", num);

 }
the damn code shouldn’t stop ? it gets looping– Fernando
Calm down, young man. In answer, I put in condition if the return was positive. In the linked documentation, you have said that, in case of this error, you would return a negative value (not specifying which negative value). In case,
EOF
is a possible negative value in 2 billion existing. Perhaps exchange this difference for a> 0
or>= 0
(I don’t remember what the 0 return means...) is better– Jefferson Quesado
The problem is that never returns < 0, always positive, I honestly do not know the problem of my check, already angry here, will never
– Fernando
Are ellipsis literal or are they just a stylistic resource to indicate that there may be more to it? If this is the second option, try making an example of minimal input and output to try playing. For example, 3 numbers would be enough
– Jefferson Quesado
Swears you uploaded the file to the mega uploads? Why not have a smaller and viable example?
– Jefferson Quesado