c++从文件读取整数

#include<stdio.h>
#include<conio.h>
#include<process.h>

main()
{
int a,i,n,sum=0;
FILE *fp;
clrscr();

//Writing numbers to the file
fp=fopen(“DATA”,”w”);
if(fp==NULL)
{
printf(“File could not open!!”);
exit(0);
}

printf(“How many numbers?”);
scanf(“%d”,&n);
printf(“Enter numbers in the file:n”);

for(i=0;i<n;++i)
{
scanf(“%d”,&a);
putw(a,fp);
}
fclose(fp);

//Reading the file and doing sum
fp=fopen(“DATA”,”r”);
if(fp==NULL)
{
printf(“File could not open!!”);
exit(0);
}

while((a=getw(fp))!=EOF)
sum+=a;

fclose(fp);

//Appending sum to the file
fp=fopen(“DATA”,”a”);
if(fp==NULL)
{
printf(“File could not open!!”);
exit(0);
}

putw(sum,fp);
fclose(fp);

//Displaying file after append
fp=fopen(“DATA”,”r”);
if(fp==NULL)
{
printf(“File could not open!!”);
exit(0);
}

printf(“nFile after append:n”);
while((a=getw(fp))!=EOF)
printf(“%d “,a);

fclose(fp);
getch();
}

#include<stdio.h>
#include<conio.h>
#include<process.h>

main()
{
int a,i,n,sum=0;
FILE *fp;
clrscr();

//Writing numbers to the file
fp=fopen(“DATA”,”w”);
if(fp==NULL)
{
printf(“ File could not open!!”);
exit(0);
}

printf(“How many numbers ?”);
scanf(“%d”,&n);
printf(“Enter numbers in the file:n”);

for(i=0;i<n;++i)
{
scanf(“%d”,&a);
putw(a,fp);
}
fclose(fp);

// Reading the file and doing sum
fp=fopen(“DATA”,”r”);
if(fp==NULL)
{
printf(“File could not open!!”);
exit(0);
}

while((a=getw(fp))!=EOF)
sum+=a;

fclose(fp);

//Appending sum to the file
fp=fopen(“DATA”,”a”);
if(fp==NULL)
{
printf(“File could not open!!”);
exit(0);
}

putw(sum,fp);
fclose(fp);

// Displaying file after append
fp=fopen(“DATA”,”r”);
if(fp==NULL)
{
printf(“File could not open!!”);
exit(0);
}

printf(“nFile after append:n”);
while((a=getw(fp))!=EOF)
printf(“%d “,a);

fclose(fp);
getch();
}

翻译自: https://www.thecrazyprogrammer.com/2013/07/c-program-that-reads-file-containing.html

c++从文件读取整数

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐