
1、编写程序,将“Hello world!”字符串输出到“hello.txt”文本文件中。 10
1、编写程序,将“Helloworld!”字符串输出到“hello.txt”文本文件中。2、以第1题中生成的“hello.txt”文件作为输入文件,编写程序,从屏幕上输出...
1、编写程序,将“Hello world!”字符串输出到“hello.txt”文本文件中。
2、以第1题中生成的“hello.txt”文件作为输入文件,编写程序,从屏幕上输出“hello.txt”文件中的内容。 展开
2、以第1题中生成的“hello.txt”文件作为输入文件,编写程序,从屏幕上输出“hello.txt”文件中的内容。 展开
展开全部
用WIN32 API 吧,也可以用C++ 的内置的库函数,看个人习惯
#include <windows.h>
HANDLE hFile = CreateFile("C:\\hello.txt",
GENERIC_WRITE,NULL,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
DWORD ret = WriteFile(hFile,"Hello World!",strlen("Hello World!"),&ret,NULL);
CloseHandle(hFile); //这是写文件
hFile = CreateFile("C:\\hello.txt",
GENERIC_READ,NULL,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
char data[MAX_PATH] = {NULL};
ret = ReadFile(hFile,&data,MAX_PATH,&ret,NULL);
printf("%s\n",data);
CloseHandle(hFile);//这是读文件,如果读的文件是大文件,建议使用内置的库函数比较好
#include <windows.h>
HANDLE hFile = CreateFile("C:\\hello.txt",
GENERIC_WRITE,NULL,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
DWORD ret = WriteFile(hFile,"Hello World!",strlen("Hello World!"),&ret,NULL);
CloseHandle(hFile); //这是写文件
hFile = CreateFile("C:\\hello.txt",
GENERIC_READ,NULL,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
char data[MAX_PATH] = {NULL};
ret = ReadFile(hFile,&data,MAX_PATH,&ret,NULL);
printf("%s\n",data);
CloseHandle(hFile);//这是读文件,如果读的文件是大文件,建议使用内置的库函数比较好
展开全部
#include<stdio.h>
readFile(char* fileName)
{
FILE* fp;
char ch;
fp = fopen(fileName, "r+");
if(fp == NULL)
{
printf("cannot open the file!\n");
getch();
}
else
{
while((ch=fgetc(fp)) != EOF)
{
printf("%c",ch);
}
fclose(fp);
getch();
}
}
writeFile(char* fileName)
{
FILE* fp;
char ch;
char* str = "hello world!";
fp = fopen(fileName, "w+");
if(fp == NULL)
{
printf("cannot open the file!\n");
getch();
}
else
{
fputs(str,fp);
fclose(fp);
getch();
}
}
main()
{
char *fileName = "hello.txt";
writeFile(fileName);
readFile(fileName);
}
readFile(char* fileName)
{
FILE* fp;
char ch;
fp = fopen(fileName, "r+");
if(fp == NULL)
{
printf("cannot open the file!\n");
getch();
}
else
{
while((ch=fgetc(fp)) != EOF)
{
printf("%c",ch);
}
fclose(fp);
getch();
}
}
writeFile(char* fileName)
{
FILE* fp;
char ch;
char* str = "hello world!";
fp = fopen(fileName, "w+");
if(fp == NULL)
{
printf("cannot open the file!\n");
getch();
}
else
{
fputs(str,fp);
fclose(fp);
getch();
}
}
main()
{
char *fileName = "hello.txt";
writeFile(fileName);
readFile(fileName);
}
追问
请用C++编程吧,谢谢!
追答
哥只会C
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询