sad_sleep
(Buồn Ngủ)
December 28, 2021, 4:06pm
1
Đề bài yêu cầu tạo 1 tệp văn bản input.txt gồm các số nguyên. Tìm số lớn nhất, bé nhất. Rồi ghi các gtri tìm được vào tệp output.txt
Em chào mn ạ. Mn có thể fix code giúp em đc k ạ. Nó chỉ hiện gtri lớn nhất thui :(((
#include<stdio.h>
void nhapmang(int a[], int *n)
{
FILE *f;
int i=0;
f=fopen("inputgt.txt","rt");
while(feof(f)==0)
{
fscanf(f,"%d",&a[i]);
if(feof(f)==0)
i++;
}
*n=i;
}
void xemmang(int a[], int n)
{
for(int i=0; i<n;i++)
printf("%5d",a[i]);
}
void maxmang(int a[], int n)
{
int ma=a[0];
for(int i=1;i<n;i++)
if(a[i]>ma)
ma=a[i];
FILE *f;
f=fopen("outputgt.txt","wt");
fprintf(f,"gia tri lon nhat la : %5d", ma);
fclose(f);
}
void minmang(int a[], int n)
{
int mi=a[0];
for(int i=1; i<n;i++)
{
if(a[i]<mi)
mi=a[i];
}
FILE *f;
f=fopen("outputgt.txt", "wt");
fprintf(f,"\nGia tri be nha la : %5d", mi);
fclose(f);
}
int main()
{
int a[30];
int n;
nhapmang(a, &n);
printf("Mang doc la :");
xemmang(a,n);
maxmang(a,n);
minmang(a,n);
}
nitro2
(Nhím)
December 29, 2021, 3:37am
2
Lần đầu tiên trong đời mình thấy mode wt luôn á
Bạn thử đọc link dưới đây rồi dùng mode khác “phù hợp” hơn xem.
The C library function FILE *fopen(const char *filename, const char *mode) opens the filename pointed to, by filename using the given mode. It opens a file and returns a pointer to a FILE object that can be used to access the file.
sad_sleep
(Buồn Ngủ)
December 29, 2021, 3:52am
3
“wt” hoặc “w” cũng đc í bạn
SITUVN.gcd
(SITUVNgcd)
December 29, 2021, 5:08am
5
Nếu muốn ghi nối tiếp (append) thì phải dùng a, hoặc a+.
https://www.cplusplus.com/reference/cstdio/fopen/
“a” append: Open file for output at the end of a file. Output operations always write data at the end of the file, expanding it. Repositioning operations (fseek , fsetpos , rewind ) are ignored. The file is created if it does not exist.
“a+” append/update: Open a file for update (both for input and output) with all output operations writing data at the end of the file. Repositioning operations (fseek , fsetpos , rewind ) affects the next input operations, but output operations move the position back to the end of file. The file is created if it does not exist.
sad_sleep
(Buồn Ngủ)
December 29, 2021, 9:39am
6
mình học trong giáo trình
w hoặc wt đều có ý nghĩa tạo ra tệp văn bản mới để ghi
library
(La biblioteca)
December 29, 2021, 2:11pm
8
Như @rogp10 đề cập, cậu có chắc cậu không nhầm dấu + với chữ t không?
lúc đầu mình đọc cái này tưởng mình kiến thức C thiển cận nên chưa học tới mod wt, chỉ biết có w với w+
sad_sleep
(Buồn Ngủ)
December 29, 2021, 2:20pm
10
thực sự là mình học trong giáo trình, giáo viên cũng nói
wt là viết tắt của write text
thế chắc do mình thiển cận thôi nhé, mình ko có ý thực sự chọc ghẹo đâu ak :))
sad_sleep
(Buồn Ngủ)
December 29, 2021, 2:24pm
12
mình mới học bên lập trình nên kiến thức cũng hạn hẹp :((((
library
(La biblioteca)
December 29, 2021, 2:29pm
13
Tớ hiểu rồi.
2 link mà @nitro2 và @SITUVN.gcd đưa cho cậu có lẽ chính xác hơn so với giáo trình của cậu.
Cậu thử sử dụng mode “a” hoặc “a+” để append vào file như @SITUVN.gcd nói xem (lý do được đề cập ở 2 comment của 2 bạn tớ đề cập ở trên)
Cá nhân tớ đọc code 2 hàm đó của cậu không thấy vấn đề gì (sorry nha, tớ không available trên PC để chạy thử code cậu).
sad_sleep
(Buồn Ngủ)
December 29, 2021, 2:33pm
14
cảm ơn mọi người ạ, mình làm đc rồi ạ <3