C program not reading text file -


i'm making program reads numbers text file, sorts them in ascending order, prints both list of numbers (in ascending order) , numbers moved around during sorting process.

the biggest issue cannot program read , print text file whole. how program read text file whole starters?

this text file: data_a5.txt

879 646 80 385 741 57 370 240 111 400 262 678 951 506 720 508 792 863 677 864 70 5 591 440 989 478 867 636 278 827 692 243 806 676 158 550 425 226 783 129 876 714 125 721 164 555 730 146 596 947 174 837 48 589 808 868 694 677 379 62 580 165 956 139 215 14 45 552 98 154 702 661 997 825 363 782 229 915 281 397 295 219 231 476 253 22 873 504 653 698 772 184 453 508 977 863 624 947 104 926 

do past answers on stack overflow referring file i/o. there numerous ways can read text file, including using fgets or getline, suggest learn (see link). don't ever use gets(). , in case, make sure file in same directory c file.

i suggest save these numbers array, , every line read in text file, sort new integer or element array after assigned current array index. problem in doing would have know how many numbers file can read ahead of time. otherwise, sort them in loop.

c read file line line

int main (void)  { file * fptr = fopen("data_a5.txt", "r"); int maxline = 4; char line[maxline] = {0}; int currentint;  if (fptr == null) {   printf("could not find file.\n");   exit(1);   }  while (fgets(line,maxline,fptr)) {   if (sscanf(line, "%d", &currentint) == 1)   // successful read   else   // unsuccessful read  printf("current line of text: %s\n", line); printf("current integer: %d\n", currentint); }  fclose(fptr);  return 0; } 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -