Programming C: Posts 14 – Import and export files in C / C ++
During the programming process, we often have to read and write data to a file. This article will guide you how to make simple with C and C ++.
Noted: In the example below, the input and output files in the same folder with the source files.
Example: Cho file input.txt, The first line consists of 1 n is the number of friends, n the next line of each line is the name of 1 friend. Output.txt file read and write the list of friends included the serial number
input.txt | output.txt |
---|---|
3 Nguyen Van Quan Nguyen Thi Hong Anh Nguyen Van Hung |
1.Nguyen Van Quan 2.Nguyen Thi Hong Anh 3.Nguyen Van Hung |
Code C
#include <stdio.h> int main() { int n, i; char name[255]; // khai bao bien ten FILE *fi = fopen("input.txt", "r"); // mo file de doc FILE *fo = fopen("output.txt", "w"); // mo file de ghi fscanf(fi, "%d", &n); // doc so n tu file fi fgets(name, 255, fi); // loai bo dau xuong dong sau khi doc so n for(i = 0; i < n; i++) { fgets(name, 255, fi); // doc chuoi gom 255 ky tu tu file fi fprintf(fo, "%d.%s", (i+1), name); // ghi chuoi ra file fo } fclose(fi); // dong file fi fclose(fo); // dong file fo return 0; }
Code C++
#include <fstream> using namespace std; int main() { int n; string name; ifstream fi("input.txt"); // mo file de doc ofstream fo("output.txt"); // mo file de ghi fi >> n; // doc 1 so tu file getline(fi, name); // loai bo dau xuong dong sau khi doc so n for (int i = 0; i <n; i++) { getline(fi, name); // doc 1 dong tu file fo << (i+1) << "." << name << '\n'; // ghi 1 dong ra file } fi.close(); fo.close(); return 0; }
how to enter data into the file?
Above have written to the file and then that way.
Suppose you have 1 INPUT.TXT file like this:
11 Abdel Martinez 5.5
20 Mas Abella 16
6 Pla Babet 10.75
his creation 1 include int id struct Sinh_vien, char ten[50], Four are[50], double diem;
and 1 array struct Sinh_vien sv[3]
Ask yourself how to retrieve data from the file to be sv INPUT.TXT[1].id = 11; sv[1].ten = ‘Martinez”; sv[1].ho = 'Abdel'; sv[1].diem = 5.5
Because I try and code it took whitespace, try ignoring their space with the following code but not:
//char *temp;
do
{
fscanf(be, “%c”, &(*(temp+j)));
j+=1;
}
while (*(temp+j) == SPACE);
free(temp);
//
Visiting teachers expect to receive sir.
I have for j = 0 which in this forgotten record
You use the% s instead of% c nhé.
fscanf(be, “%d”, &sv.id);
fscanf(be, “%with”, &sv.ten);
fscanf(be, “%with”, &sv.ho);
fscanf(be, “%d”, &Sv.Duje);
I tried your way but it took space between id and they should wrong results
So you try this:
fscanf(be, “%d %s %s %d”, &sv.id, &sv.ten, &sv.ho, &Sv.Duje); // (copy paste nhé, space between% d and% s)
Still wrong as the original. You can send your code via email, you see my household is not? Thank you offline
Chat with me via fb.com/nguyenvanquan7826 nhé.
I created a program student information from the keyboard,but still that program information is stored on a file and when exported, export information on file, they must do so a.em code in C ++, you help me sir,I'm folding a.Cam Thank you!!!
Then I export to file only.
Ban nguyenvanquan7826 yourself questions, How correct any location in the file in C ++, right. thanks.
I run my code is read DC k n, print it out n = 0 Test, replace n with the value determined is why he is running DC?
You must have a data file nhé.
I have a file and data INPUT.TXT la is the function chain:
x=1;
y = x + 3;
x=12;
how they can call each rod string in the file check each string to check some conditions that string right or wrong view and then print out and check the next sequence until all dc file sir?
p/s:
I want to use the function fgets that does it all in the entire file whip each examination
in the example in the article I have read every line that you.
you should have no false c getline de read each line.
also read each line that gets.
int main()
{
char statement[50];
FILE * fin;
fin=fopen(“input.txt”, “r”);
while (fgets(statement, 50, end) !=NULL)
{
puts(statement);
int check= CORRECT(statement);
if(check==0)
printf(“This statement is illegal”);
else
printf(“This statement is legal”);
}
printf(“\n”);
return 0;
}
I write code, but it must print until het Reach 50 ki tu each examination, But if you want to check each line ends with ; and check dong by roi moi ties piled under a star contest
Read so read each line and then you.
a stomach for e ask sir
e moved from pascal to c ++
if pascal click “input.txt” and press Ctrl + Enter, it will be the input file. So in C ++ is how ạ
thank you
Manually open by hand alone e.
let ask r with w in C code that means j
read and write.
Nguyenvanquan7826, Let me ask why I did it again, but why did it read the file and print the file?.
do not understand your question.
Let me ask is the line fgets(name, 255, be); First of all, I don't understand what it means,why is it like reading string from File.Thank youEm
then it is reading string from file file.