C Program To Read Int From File C++, C Program To Read Numbers From Text File

12 5 64 27 9I can use the fscanf function to read the first 3 values and store them in 3 variables. But I can”t read the rest.I tried using the fseek function, but it works only on binary files.

Đang xem: Read int from file c++

Please help me store all the values in integer variables.

*

A simple solution using fscanf:

void read_ints (const char* file_name){ FILE* file = fopen (file_name, “r”); int i = 0; fscanf (file, “%d”, &i); while (!feof (file)) { printf (“%d “, i); fscanf (file, “%d”, &i); } fclose (file); }

*

How about this?

fscanf(file,”%d %d %d %d %d %d %d”,&line1_1,&line1_2, &line1_3, &line2_1, &line2_2, &line3_1, &line3_2); In this case spaces in fscanf match multiple occurrences of any whitespace until the next token in found.

Read more: watch wow wow wubbzy online free

*

*

Thanks for contributing an answer to Stack Overflow!

Please be sure to answer the question. Provide details and share your research!

But avoid

Asking for help, clarification, or responding to other answers.Making statements based on opinion; back them up with references or personal experience.

Read more: how do you say how do you say in spanish

To learn more, see our tips on writing great answers.

Post Your Answer Discard

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged c file file-io scanf line or ask your own question.

c function to read character by character from a txt file, make it into int and store it in a linked list
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev2021.5.14.39313

Your privacy

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Leave a Comment