有如下程序 #include <stdio.h> void main() { FILE *fp; int i=20,j=30,k,n; fp=fopen("data.dat","w"); fprintf(fp,"%d\n",i);fprintf(fp,"%d\n",j); fclose(fp); fp=fopen("data.dat", "r"); fscanf(fp,"%d%d",&k,&n); printf("%d %d\n",k,n); fclose(fp); } 程序运行后的输出结果是 。
时间: 2023-11-03 17:05:44 浏览: 146
程序运行后的输出结果是:
20 30
程序首先将 i 和 j 的值分别写入到文件 data.dat 中,然后再从该文件中读取两个整数 k 和 n 并将它们输出到屏幕上。由于写入文件时 i 和 j 分别占据一行,因此在读取时需要使用两个 %d 分别读取 i 和 j 的值。最终输出结果是 20 30。
相关问题
C语言程序填空:从键盘输入10个字符,将其全部输出到一个磁盘文件“data.dat"中保存起来#include<stdio.h> #include<conio.h> #include<stdlib.h> void main() { FILE *fp; int num; int i=0; if((fp=fopen("data.dat","w"))==NULL) { printf("打不开文件\n"); exit(0); } while(i<=9) { fprintf(fp,"%d",num); i++; } }
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main() {
FILE *fp;
char ch[10];
int i=0;
if((fp=fopen("data.dat","w"))==NULL) {
printf("打不开文件\n");
exit(0);
}
printf("请输入10个字符:\n");
while(i<=9) {
ch[i] = getch();
fprintf(fp,"%c",ch[i]);
i++;
}
fclose(fp);
printf("已将输入的字符保存到文件data.dat中!");
}
注意:程序中使用了 getch() 函数,需要包含 conio.h 头文件。另外,文件操作完成后需要关闭文件,使用 fclose() 函数。
1.创建文件夹: #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <iostream> using namespace std; int main() { string folder_name = "new_folder"; mkdir(folder_name.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); //创建文件夹 return 0; } 2.复制文件: #include <stdio.h> #include <stdlib.h> int main() { FILE *fp1, *fp2; //定义两个文件指针 char ch; fp1 = fopen("file1.txt", "r"); //打开要复制的文件 fp2 = fopen("file2.txt", "w"); //打开要复制到的文件 while ((ch = fgetc(fp1)) != EOF) { fputc(ch, fp2); //复制文件 } fclose(fp1); fclose(fp2); return 0; } 3.移动文件: #include <stdio.h> #include <stdlib.h> int main() { char old_path[100] = "old_folder/file1.txt"; char new_path[100] = "new_folder/file1.txt"; int result = rename(old_path, new_path); //移动文件 if (result == 0) { printf("移动成功\n"); } else { printf("移动失败\n"); } return 0; } 4.删除文件夹: #include <unistd.h> #include <stdio.h> int main() { char folder_name[100] = "new_folder"; int result = rmdir(folder_name); //删除文件夹 if (result == 0) { printf("删除成功\n"); } else { printf("删除失败\n"); } return 0; } 5.显示文件夹中的内容: #include <dirent.h> #include <stdio.h> int main() { DIR *dir; struct dirent *ent; char folder_name[100] = "new_folder"; dir = opendir(folder_name); //打开文件夹 while ((ent = readdir(dir)) != NULL) { printf("%s\n", ent->d_name); //遍历文件夹中的文件 } closedir(dir); return 0; } 6.查看文件内容: #include <stdio.h> int main() { FILE *fp; char ch; fp = fopen("file1.txt", "r"); //打开文件 while ((ch = fgetc(fp)) != EOF) { printf("%c", ch); //输出文件内容 } fclose(fp); return 0; } 7.修改文件权限: #include <sys/stat.h> #include <stdio.h> int main() { char file_name[100] = "file1.txt"; chmod(file_name, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH); //修改文件权限 return 0; } 8.搜索文件: #include <dirent.h> #include <stdio.h> #include <string.h> int main() { DIR *dir; struct dirent *ent; char folder_name[100] = "new_folder"; char search_name[100] = "file1.txt"; dir = opendir(folder_name); //打开文件夹 while ((ent = readdir(dir)) != NULL) { if (strcmp(ent->d_name, search_name) == 0) //搜索文件 { printf("找到文件:%s\n", ent->d_name); break; } } closedir(dir); return 0; }将上述代码整合成一个完整的程序代码
以下是将上述代码整合成一个完整的程序代码:
```
#include <iostream>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
using namespace std;
// 1.创建文件夹
void create_folder()
{
string folder_name = "new_folder";
mkdir(folder_name.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); //创建文件夹
}
// 2.复制文件
void copy_file()
{
FILE *fp1, *fp2; //定义两个文件指针
char ch;
fp1 = fopen("file1.txt", "r"); //打开要复制的文件
fp2 = fopen("file2.txt", "w"); //打开要复制到的文件
while ((ch = fgetc(fp1)) != EOF)
{
fputc(ch, fp2); //复制文件
}
fclose(fp1);
fclose(fp2);
}
// 3.移动文件
void move_file()
{
char old_path[100] = "old_folder/file1.txt";
char new_path[100] = "new_folder/file1.txt";
int result = rename(old_path, new_path); //移动文件
if (result == 0)
{
printf("移动成功\n");
}
else
{
printf("移动失败\n");
}
}
// 4.删除文件夹
void delete_folder()
{
char folder_name[100] = "new_folder";
int result = rmdir(folder_name); //删除文件夹
if (result == 0)
{
printf("删除成功\n");
}
else
{
printf("删除失败\n");
}
}
// 5.显示文件夹中的内容
void show_folder_content()
{
DIR *dir;
struct dirent *ent;
char folder_name[100] = "new_folder";
dir = opendir(folder_name); //打开文件夹
while ((ent = readdir(dir)) != NULL)
{
printf("%s\n", ent->d_name); //遍历文件夹中的文件
}
closedir(dir);
}
// 6.查看文件内容
void show_file_content()
{
FILE *fp;
char ch;
fp = fopen("file1.txt", "r"); //打开文件
while ((ch = fgetc(fp)) != EOF)
{
printf("%c", ch); //输出文件内容
}
fclose(fp);
}
// 7.修改文件权限
void change_file_permission()
{
char file_name[100] = "file1.txt";
chmod(file_name, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH); //修改文件权限
}
// 8.搜索文件
void search_file()
{
DIR *dir;
struct dirent *ent;
char folder_name[100] = "new_folder";
char search_name[100] = "file1.txt";
dir = opendir(folder_name); //打开文件夹
while ((ent = readdir(dir)) != NULL)
{
if (strcmp(ent->d_name, search_name) == 0) //搜索文件
{
printf("找到文件:%s\n", ent->d_name);
break;
}
}
closedir(dir);
}
int main()
{
create_folder();
copy_file();
move_file();
delete_folder();
show_folder_content();
show_file_content();
change_file_permission();
search_file();
return 0;
}
```
阅读全文