How to store only characters from a text file in an array?

Asked

Viewed 47 times

1

int main(){
char aux;
int i=0,j=0,p=0;
char vetor[26];

FILE *arq;

My.txt file has any string, I just want to take the characters of this string and move to a 26 position vector, which is the amount of letters you have in the alphabet. It is not the case now, but in the future I will implode a function to have no repeated characters.

arq = fopen("arquivo.txt","r");

No while I scan the entire text file and remove the ' n' and store the rest in the vector

while (fscanf(arq, "%c", &aux )!= EOF){
    printf("%d %c\n",i++ ,aux);
    if(aux != '\n'){
       vetor[j] = aux;
       j++;
    }
}
printf("Print do vetor\n");

However, at the time I’m going to print all the positions of the vector, there are still some empty spaces that I don’t know what it is, and I don’t know how to treat them. Follow a screenshot below

for ( p = 0; p < j; p++){
    printf("%c\n", vetor[p]);
}


fclose(arq);
return 0;

} inserir a descrição da imagem aqui

  • How’s the original file (.txt file) ?

  • AABCDD BADCC FABD XFCX

  • each space is a line break(unable to send with line break)

  • Testing here on my pc appears correct. You are sure you have normal line breaks in your input file ?

  • yes, have 4 lines and each row has a string

  • strange , but obg by the time , I will try to see calmly here , vlw

  • vlw mano, was what I was weighing, just gave a space between " and % in the function fscanf

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.