1
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
int main()
{
FILE* fp1, * fp2;
fp1 = fopen("File1.in", "r");
fp2 = fopen("File2.out", "w");
while (!feof(fp1))
{
int c = fgetc(fp1);
fputc(c, fp2);
}
fclose(fp1);
fclose(fp2);
return 0;
}
2
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
int main()
{
FILE* fp1, * fp2;
int count = 0;
fp1 = fopen("File1.in", "r");
while (!feof(fp1))
{
fgetc(fp1);
count++;
}
fclose(fp1);
cout << count - 1; // 最后\0不算
return 0;
}
3
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
int main()
{
FILE* fp;
fp = fopen("File1.in", "r");
while (!feof(fp))
{
int c = fgetc(fp);
cout << static_cast<char>(c);
}
fclose(fp);
return 0;
}
4
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
int main()
{
FILE* fp;
fp = fopen("File1.in", "a");
if (!fp)
exit(0);
fprintf(fp, "\nAppend a line...");
fclose(fp);
return 0;
}
5
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#define BUF_SIZE 256
char buffer[BUF_SIZE];
int main()
{
FILE* fp, * fp2;
fp = fopen("File1.in", "r+");
fp2 = fopen("File2.out", "w");
if (!fp)
exit(0);
const char* predicate = "Grape\n";
while (fgets(buffer, sizeof(buffer), fp))
{
if (!strcmp(buffer, predicate))
continue;
fputs(buffer, fp2);
}
fclose(fp);
return 0;
}
6
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#define BUF_SIZE 256
char buffer[BUF_SIZE];
int main()
{
FILE* fp;
fp = fopen("File1.in", "rb");
if (!fp)
exit(0);
fseek(fp, 0, SEEK_END);
long bytes = ftell(fp);
cout << bytes << "bytes";
fclose(fp);
return 0;
}
7
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#define BUF_SIZE 256
char buffer[BUF_SIZE];
int main()
{
FILE* fp;
fp = fopen("students.txt", "w+");
if (!fp)
exit(0);
fprintf(fp, "%20s%15s%10s%10s%10s%10s\n", "Name", "Number", "Chinese", "Maths", "English", "Average");
char name[20],
number[10];
int chinese,
maths,
english;
while (fscanf(stdin, "%s %s %d %d %d",
name, &number, &chinese, &maths, &english) == 5)
{
float avg = (chinese + maths + english) / 3.0;
fprintf(fp, "%20s%15s%10d%10d%10d%10.2f\n", name, number, chinese, maths, english, avg);
}
fclose(fp);
return 0;
}