难以使用malloc和scanf

难以使用malloc和scanf,c,C,我很难使用malloc和fscanf。 我只想读取一个文件并使用 执行此代码时,我遇到了一个分段错误。 我不确定我做错了什么。如果有人指出解决办法,我将不胜感激。 这是我的密码: #include <stdio.h> #include <stdlib.h> int main(int argc, char* argv[]){ char* buffer = (char*)malloc(*argv[1]); // Allocate memory if(bu

我很难使用
malloc
fscanf
。 我只想读取一个文件并使用 执行此代码时,我遇到了一个分段错误。 我不确定我做错了什么。如果有人指出解决办法,我将不胜感激。 这是我的密码:

#include <stdio.h> 
#include <stdlib.h>

int main(int argc, char* argv[]){
    char* buffer = (char*)malloc(*argv[1]); // Allocate memory 
    if(buffer=NULL) // if *argv[1] is not existed, exit the program    
        exit(1);

    int n = 0;
    FILE* fp=fopen("file.txt","r"); // Open the file
    do {
        buffer[n] =fscanf(fp,"%c",buffer); // read from file until it gets EOF
    } while(buffer[n] != EOF);
    fclose(fp); // Close the file

    printf("%s",buffer); // Print the stored string

    free(buffer); // Return the memory
    return 0;    
}
#包括
#包括
int main(int argc,char*argv[]){
char*buffer=(char*)malloc(*argv[1]);//分配内存
如果(buffer=NULL)//如果*argv[1]不存在,则退出程序
出口(1);
int n=0;
FILE*fp=fopen(“FILE.txt”,“r”);//打开文件
做{
buffer[n]=fscanf(fp,“%c”,buffer);//从文件中读取,直到它获得EOF
}while(缓冲区[n]!=EOF);
fclose(fp);//关闭文件
printf(“%s”,buffer);//打印存储的字符串
释放(缓冲区);//返回内存
返回0;
}
这似乎是错误的:

   char* buffer = (char*)malloc(*argv[1]);
命令行参数是一个字符串,但您需要一个数字。您必须先将字符串转换为数字

另一个问题:在循环中,n永远不会增加,这就是为什么只写入缓冲区的第一个字节。

明白了。这:

if(buffer=NULL)
应该是这样的:

if(buffer==NULL)
您正在将
buffer
设置为
NULL
。我相信你能看到接下来会发生什么


更一般地说,这段代码试图做一些事情,并且充满了bug。您应该已经分别测试了不同的函数,并解决了这些错误。

请在线查找修复代码和注释:

#include <stdio.h> 
#include <stdlib.h> 

int main(int argc, char* argv[])
{
  // Add the check if *argv[1] does not exist, exit the program    
  long mem_sz=strtol(argv[1],NULL,10);//Safer to atoi
  char* buffer = (char*)malloc(mem_sz); // properly allocate memory 

  //You missed the == in the NULL check.
  if(buffer==NULL) 
   exit(1);

  int n = 0; 
  FILE* fp=fopen("file.txt","r"); // Open the file
  if (fp == NULL)//Check fp too
   exit(1);

  do 
  { 
    buffer[n++]=fscanf(fp,"%c",buffer);
  } // read from file until it gets EOF and n++
  while(buffer[n-1] != EOF);//Check the last read character

  buffer[n]=0;//Put an end of string, so that printf later will work correct

  fclose(fp); // Close the file
  printf("%s\n",buffer); // Print the stored string
  free(buffer); // Return the memory
  return 0;    
}
#包括
#包括
int main(int argc,char*argv[])
{
//添加检查如果*argv[1]不存在,则退出程序
long mem_sz=strtol(argv[1],NULL,10);//比atoi更安全
char*buffer=(char*)malloc(mem_sz);//正确分配内存
//您在空检查中错过了==。
if(buffer==NULL)
出口(1);
int n=0;
FILE*fp=fopen(“FILE.txt”,“r”);//打开文件
if(fp==NULL)//也检查fp
出口(1);
做
{ 
缓冲区[n++]=fscanf(fp,“%c”,缓冲区);
}//从文件中读取,直到获得EOF和n++
while(buffer[n-1]!=EOF);//检查最后读取的字符
buffer[n]=0;//在字符串末尾加一个字符,以便printf以后可以正常工作
fclose(fp);//关闭文件
printf(“%s\n”,buffer);//打印存储的字符串
释放(缓冲区);//返回内存
返回0;
}

运行时使用的命令行参数是什么?表达式
*argv[1]
返回
argv[1]
中字符串的第一个字符,如果是数字,则该字符串的值介于48(
'0'
)和57(
'9'
)之间。此外,使用大小为零的
malloc
可能会返回有效指针。您还可以在
if
语句中将
NULL
赋值给
buffer
=
=
之间的差异)。而
scanf
函数族不会返回扫描的值,而是返回转换后的值。您永远不会增加
n
。实际上,如果argv[1]不存在,您将得到一个segfault,因为您在访问它之后才检查它是否存在。
char*buffer=(char*)malloc(*argv[1]);//分配内存
——这一行的意义是什么?您希望argv[1]是什么?