C 读取和写入文件打开()读取()
我需要制作一个程序来读取一个文件(arg[1]),并显示它的内容,但是要有一个特定的字节数[arg2]。如果未指定字节,则将打印整个文件C 读取和写入文件打开()读取(),c,file,byte,C,File,Byte,我需要制作一个程序来读取一个文件(arg[1]),并显示它的内容,但是要有一个特定的字节数[arg2]。如果未指定字节,则将打印整个文件 void read(char *arg[]){ int fh; char buffer[100]; int rd; fh = open(arg[1],O_RDONLY); printf ("Printing %s\n",arg[1]); while (rd = r
void read(char *arg[]){
int fh;
char buffer[100];
int rd;
fh = open(arg[1],O_RDONLY);
printf ("Printing %s\n",arg[1]);
while (rd = read(fh,buffer,100)) {
buffer[rd] = '\0';
printf("%s",buffer);
}
}
我至少需要使用open(),read()。
有人能帮我吗?#包括
#include <stdio.h>
#include <stdlib.h>
void print(char* buff, int size)
{
int i;
for(i = 0; i < size ; i++)
{
printf("%c",buff[i]);
}
printf("\n");
}
int main(int argc, char* argv[])
{
FILE * pFile;
long lSize;
char * buffer;
size_t result;
pFile = open ( argv[1] , O_RDONLY );
if (pFile==NULL) {fputs ("File error",stderr); exit (1);}
if(argv[2])
{
lSize = argv[2];
}
else
{
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);
}
buffer = (char*) malloc (sizeof(char)*lSize);
if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}
result = read (pFile,buffer,lSize);
if (result != lSize) {fputs ("Reading error",stderr); exit (3);}
print(buffer,lSize);
fclose (pFile);
free (buffer);
return 0;
}
#包括
无效打印(字符*浅黄色,整数大小)
{
int i;
对于(i=0;i
对您编写的程序几乎没有观察到
void read(char*arg[])
这里从main()函数传递文件名,然后需要用字符指针数组捕捉文件名,用单个字符指针捕捉就足够了。因此,修改它就像voidread(char*arg)
open()
,read()
,stat()
系统调用的手册页
这是我对你的要求的解释
#include <stdio.h>
#include <malloc.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
void my_read(char *arg1){
int fh;
// char buffer[100];//don't use static array bcz we don't know how much data is there in file, create it dynamically
int rd,n;
fh = open(arg1,O_RDONLY);
if(fh == -1)
{
perror("open");
return;
}
else
{
//first find the size of file .. use stat() system call
struct stat v;
stat(arg1,&v);//storing all file related information into v
int size = v.st_size;//st.szie is a member of stat structure and it holds size of file
//create dynamic array equal to file size
char *p = malloc(size * sizeof(char));
printf("enter no of bytes you want to read :\n");
scanf("%d",&n);
if(n==0)
{
//read data from file and copy into dynamic array and print
int ret = read(fh,p,size);
if(ret == -1)
{
perror("read");
return ;
}
else
{
printf("data readed from file is :\n");
printf("%s\n",p);
}
}
else
{
//read data from file and copy into dynamic array and print
int ret = read(fh,p,n);
if(ret == -1)
{
perror("read");
return ;
}
else
{
printf("data readed from file is :\n");
printf("%s\n",p);
}
}
if (close(fh) < 0)
{
perror("c1");
return;
}
}
int main(int argc,char *argv[])
{
char f_name[100];
printf("enter the file name :\n");
scanf("%s",f_name);
my_read(f_name);
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
作废我的读取(字符*arg1){
int fh;
//char buffer[100];//不要使用静态数组bcz我们不知道文件中有多少数据,请动态创建它
int rd,n;
fh=打开(仅arg1,Ordu);
如果(fh==-1)
{
佩罗(“公开”);
返回;
}
其他的
{
//首先找到文件的大小..使用stat()系统调用
结构统计v;
stat(arg1,&v);//将所有与文件相关的信息存储到v中
int size=v.st_size;//st.szie是stat结构的成员,它保存文件的大小
//创建等于文件大小的动态数组
char*p=malloc(大小*sizeof(char));
printf(“输入要读取的字节数:\n”);
scanf(“%d”和“&n”);
如果(n==0)
{
//从文件中读取数据并复制到动态数组中并打印
int ret=读取(fh、p、大小);
如果(ret==-1)
{
佩罗(“阅读”);
返回;
}
其他的
{
printf(“从文件读取的数据是:\n”);
printf(“%s\n”,p);
}
}
其他的
{
//从文件中读取数据并复制到动态数组中并打印
int ret=读取(fh,p,n);
如果(ret==-1)
{
佩罗(“阅读”);
返回;
}
其他的
{
printf(“从文件读取的数据是:\n”);
printf(“%s\n”,p);
}
}
如果(关闭(fh)<0)
{
perror(“c1”);
返回;
}
}
int main(int argc,char*argv[])
{
字符f_名称[100];
printf(“输入文件名:\n”);
scanf(“%s”,f_名称);
我的名字;
}
什么是arg2?要从文件中读取的最大字节数?以及..您是指整个文件中的所有文件吗?这在arg1中指定。?在arg[1]中您指定了文件名,arg2是要读取的字节,是的。是的,我指的是整个文件,对不起我的英语。好的,我仍然感到困惑。您已经从文件中读取了。现在还剩下什么?限制仅从文件中读取arg[2]字节?哦,FFS。发布您使用的真实代码:而不是“类似于我的代码的东西”.浪费时间。无法使用fopen为您修复感谢您的帮助,但我使用了另一个答案的代码。我不能放弃投票,因为我的代表数不到15。与以前的fopen版本混淆:if(pFile==NULL)
.0是有效的fd,失败时打开返回-1。有许多后续错误…非常完美,谢谢!!应该是int-fd
,而不是int-fh
`while (rd = read(fh,buffer,100)) { //some code }` why rotating loop ? you can read entire data of file at a time, for that use `stat()` system call and find the size of file and then create dynamic array equivalent to file size and then read whole/particular data using `read()` system call.
#include <stdio.h>
#include <malloc.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
void my_read(char *arg1){
int fh;
// char buffer[100];//don't use static array bcz we don't know how much data is there in file, create it dynamically
int rd,n;
fh = open(arg1,O_RDONLY);
if(fh == -1)
{
perror("open");
return;
}
else
{
//first find the size of file .. use stat() system call
struct stat v;
stat(arg1,&v);//storing all file related information into v
int size = v.st_size;//st.szie is a member of stat structure and it holds size of file
//create dynamic array equal to file size
char *p = malloc(size * sizeof(char));
printf("enter no of bytes you want to read :\n");
scanf("%d",&n);
if(n==0)
{
//read data from file and copy into dynamic array and print
int ret = read(fh,p,size);
if(ret == -1)
{
perror("read");
return ;
}
else
{
printf("data readed from file is :\n");
printf("%s\n",p);
}
}
else
{
//read data from file and copy into dynamic array and print
int ret = read(fh,p,n);
if(ret == -1)
{
perror("read");
return ;
}
else
{
printf("data readed from file is :\n");
printf("%s\n",p);
}
}
if (close(fh) < 0)
{
perror("c1");
return;
}
}
int main(int argc,char *argv[])
{
char f_name[100];
printf("enter the file name :\n");
scanf("%s",f_name);
my_read(f_name);
}