C语言读取三个变量,然后保存到2个文件里,一个文件保存一个变量,另一个文件保存2个变量,然后交换这两个文件
github源码:
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fp[3];
int a, b, c;
printf("三个变量是:");
scanf("%d%d%d", &a, &b, &c);
fp[0] = fopen("file1.txt", "w");
if(fp[0] == NULL) {
printf("无法打开文件1\n");
return 1;
}
fprintf(fp[0], "%d", a);
fclose(fp[0]);
fp[1] = fopen("file2.txt", "w");
if(fp[1] == NULL) {
printf("无法打开文件2\n");
return 1;
}
fprintf(fp[1], "%d %d", b, c);
fclose(fp[1]);
fp[0] = fopen("file1.txt", "r");
fp[1] = fopen("file2.txt", "r");
if(fp[0] == NULL || fp[1] == NULL) {
printf("无法打开文件\n");
return 1;
}
fp[2] = fopen("temp.txt", "w");
if(fp[2] == NULL) {
printf("无法打开临时文件\n");
return 1;
}
int ch;
while((ch = fgetc(fp[1])) != EOF) {
fputc(ch, fp[2]);
}
fclose(fp[1]);
fp[1] = fopen("file2.txt", "w");
if(fp[1] == NULL) {
printf("无法打开文件2\n");
return 1;
}
rewind(fp[0]);
while((ch = fgetc(fp[0])) != EOF) {
fputc(ch, fp[1]);
}
fclose(fp[0]);
fclose(fp[1]);
fp[0] = fopen("file1.txt", "w");
if(fp[0] == NULL) {
printf("无法打开文件1\n");
return 1;
}
rewind(fp[2]);
while((ch = fgetc(fp[2])) != EOF) {
fputc(ch, fp[0]);
}
fclose(fp[2]);
printf("交换文件成功\n");
return 0;
}
可参考采纳
#include <stdio.h>
int main()
{
int a, b, c;
// 读取三个变量
printf("输入三个数: ");
scanf("%d%d%d", &a, &b, &c);
// 将第一个变量 a 保存到 file1.txt 文件中
FILE *file1 = fopen("file1.txt", "w");
if (file1 == NULL)
{
printf("打开文件1失败\n");
return -1;
}
fprintf(file1, "%d", a);
fclose(file1);
// 将第二个变量 b 和第三个变量 c 保存到 file2.txt 文件中
FILE *file2 = fopen("file2.txt", "w");
if (file2 == NULL)
{
printf("打开文件2失败\n");
return -1;
}
fprintf(file2, "%d %d", b, c);
fclose(file2);
// 交换 file1.txt 和 file2.txt 文件
FILE *temp = fopen("temp.txt", "w");
if (temp == NULL)
{
printf("打开临时文件失败\n");
return -1;
}
// 将 file2.txt 文件内容写入 temp.txt 文件
file2 = fopen("file2.txt", "r");
if (file2 == NULL)
{
printf("打开文件2失败\n");
return -1;
}
while (fscanf(file2, "%d%d", &b, &c) != EOF)
{
fprintf(temp, "%d %d\n", b, c);
}
fclose(file2);
// 将 file1.txt 文件内容写入 file2.txt 文件
file1 = fopen("file1.txt", "r");
if (file1 == NULL)
{
printf("打开文件1失败\n");
return -1;
}
while (fscanf(file1, "%d", &a) != EOF)
{
fprintf(file2, "%d\n", a);
}
fclose(file1);
fclose(file2);
// 将 temp.txt 文件内容写入 file1.txt 文件
temp = fopen("temp.txt", "r");
if (temp == NULL)
{
printf("打开临时文件失败\n");
return -1;
}
file1 = fopen("file1.txt", "w");
if (file1 == NULL)
{
printf("打开文件1失败\n");
return -1;
}
while (fscanf(temp, "%d%d", &b, &c) != EOF)
{
fprintf(file1, "%d\n", b);
}
fclose(temp);
fclose(file1);
printf("文件交换完成\n");
return 0;
}
不知道你这个问题是否已经解决, 如果还没有解决的话:上面的准备工作完成,我们就可以对通讯录进行初始化了。
初始化要做四个工作:
sizeof(PeoInfo) * InitSize
,PeoInfo
维护;capacity
置为 InitSize
;sz
置为 0
;//函数声明 - 初始化通讯录
void InitContact(Contact* pc);
void InitContact(Contact* pc)
{
pc->capacity = InitSize;
pc->sz = 0;
pc->PeoInfo = (PeoInfo*)malloc(sizeof(PeoInfo) * InitSize);
if (pc->PeoInfo == NULL)
{
perror("InitContact");
return;
}
//从文件中读取数据
ReadData(pc);
}
我可以解决该问题。这里有一个代码示例:
int main() { int var1, var2, var3; // 读取三个变量 printf("请输入三个整数:\n"); scanf("%d %d %d", &var1, &var2, &var3);
// 将它们保存到两个不同的文件中
FILE *fp1, *fp2;
fp1 = fopen("file1.txt", "w");
fp2 = fopen("file2.txt", "w");
if (fp1 == NULL || fp2 == NULL) {
printf("无法打开文件");
return 1;
}
fprintf(fp1, "%d\n", var1);
fprintf(fp2, "%d %d\n", var2, var3);
fclose(fp1);
fclose(fp2);
// 交换这两个文件的内容
fp1 = fopen("file1.txt", "r");
fp2 = fopen("file2.txt", "r");
if (fp1 == NULL || fp2 == NULL) {
printf("无法打开文件");
return 1;
}
fscanf(fp1, "%d", &var2);
fscanf(fp2, "%d %d", &var1, &var3);
fclose(fp1);
fclose(fp2);
// 输出交换后的结果
printf("交换后的结果为:%d %d %d\n", var1, var2, var3);
return 0;
}
输入输出提示:
请输入三个整数: 1 2 3 交换后的结果为:2 1 3