The task is as follows.

Open a file containing ten numbers, read them and show them in the dialogue. Create a new file, and write to it the sum of these numbers.

The question is this:

  1. How to read numbers from a file for further operations with them.
  2. How to write to a file

That's what I did (incomplete version of the program, but working)

#include "stdafx.h" #include <stdio.h> #include <windows.h> int _tmain(int argc, _TCHAR * argv[]) { SetConsoleCP(1251); SetConsoleOutputCP(1251); FILE *fp = fopen("test1.txt", "r"); if (NULL == fp) { printf("Не удалось открыть файл!n"); return 0; } char str[24] = ""; while (!feof(fp)) { fgets(str, 24, fp); printf("%s", str); printf("n"); } fclose(fp); system("pause"); getchar(); return 0; } 

PS already tortured with this VisualStudio C ++: there is no information and examples of tasks, and if there is, then there are so incomprehensible characters that you can break your head.

    2 answers 2

    How to read numbers from a file for further operations with them.

    How and what numbers are recorded in the file? If a space and integer, then:

     ifstream f("имя_файла.txt", "r"); // открыть файл для чтения vector<int> numbers; // вектор куда будем записывать считанное copy(istream_iterator<int>(f), istream_iterator<int>(), back_inserter<vector<int> >(numbers)); // непосредственно считывание из файла и запись в вектор 

    How to write to a file

    Similarly:

     ofstream f("имя_файла.txt", "w"); // открыть файл на запись copy(numbers.begin(), numbers.end(), ostream_iterator<int>(f, " ")); // непосредственно запись из вектора в файл 

    it is assumed that numbers are in the numbers vector (although if the vector is empty, the code will also work correctly).

    • No, I do not understand anything copy (istream_iterator <int> (f), istream_iterator <int> (), back_inserter <vector <int>> (numbers)); --- what is the string for what it is? Which vector are we copying? - ek8800
    • in vector numbers . copy is the function of the standard library, the first parameter is the beginning of the copied range, the second is the end of the cop. range, the third - the beginning of the range, where we copy. - fogbit

    Your version of the program reads the string char, in other words, the characters, it displays them correctly, but it will not add, as it recognizes as a literal string. It is best to read fscanf into the buffer one digit at a time in this situation.

     int A; fscanf(A[i],"%d",fp); 

    Just why do you have 24 characters per line read? Then, when you write to a new file, open it on the record and write there the sum

     FILE *fout = fopen("test2.txt", "w"); int C=0; for (i=0;i<=9,i++) C+=A[i]; fprintf(fout,"%d",C); 

    and close the file.

    • at the same time, your 10 digits must be written as a space, the space separates one% d from the other (the comma can be used as a fraction) - student
    • I do not understand where to me this line? In a loop or instead of char? If I put in a cycle, then the program gives 3 errors. Or here at the beginning you need to radically change everything? fscanf (A [i], "% d", fp); - ek8800
    • I understand that the program reads this as a picture. Only I have no idea how to do it differently. Do you need additional libraries? And with what functions? - ek8800
    • count the number of elements in the file int j = 0; while (! feof (fp)) {if (fgetc == '') j ++;} // you have 10 of them as I understand it, i.e. from 0 to 9 inside the while loop, add a loop for array A for (i = 0; i <= 9, i ++) double A [10]; rewind fp; while (! feof (fp)) {for (i = 0; i <= j, i ++) // for you in a similar way (i = 0; i <= 9, i ++) fscanf (A [i], "% d" , fp); printf ("% d", A [i]); // printf ("n"); } something like this. - Student
    • sorry, there fscanf (A [i], "% d", fp), on the contrary, you indicate it seems, first the file, then the buffer fscanf (fp, "% d", A [i]) - student