如何使用字符串打开二进制文件?使用C

发布于 2024-12-01 23:52:33 字数 1744 浏览 1 评论 0原文

我的印象是,要使用字符串打开二进制文件,您可以简单地创建字符串,然后将其实现为将读取字符串的文件的名称。这就是我的讲义所说的。然而我显然错过了一些东西。我在 fopen 中使用了 &name, name, &name[SIZE] ,每次我都得到 inBinFile == NULL ,除非我使用注释行。我的字符串是正确的。怎么了?非常感谢您的帮助。提前致谢。

#include <stdio.h>  
#include <stdlib.h> 
#define SIZE 25

int frstmenu(void);  
int sndmenu(void);  

int main()  
{  

    int fmenu, smenu;  
    char name[SIZE];  
    FILE *inBinFile;    
    unsigned char numRead;

  fmenu = frstmenu();
  if ( fmenu !=1 && fmenu !=2 )
  {
        printf("\nIncorrect option\n");
        fmenu = frstmenu();
  }

  if (fmenu == 1)
  {    
       printf("\nEnter the file name: \n");
       scanf("%s", &name[SIZE]);
       /* printf("filename: %s", &name[SIZE]); */

       smenu = sndmenu();

       if (smenu !=1 && smenu !=2 )
       {
           printf("\nIncorrect option\n");
           smenu = sndmenu();
       }
       if (smenu == 1)
       {            

           inBinFile = fopen( name, "rb");
        /* inBinFile = fopen( "stream.grc", "rb"); */

        if (inBinFile == NULL)
        {
          fprintf(stderr, "Error opening %s", &name[SIZE]);
          return(-1);

        fclose(inBinFile);
       }      
  }
  return(0);
}

int frstmenu()
{

  float selection;

  printf("----Menu----\n");
  printf("1 Open a file ( supported format: .grc )\n"); 
  printf("2 Exit the program\n");
  printf("  Please select an option (1 or 2): ");
  scanf("%f", &selection);

  return(selection);

}

int sndmenu()

{

int selection;

 printf("---Menu---\n");
 printf("1 Decode the sequence\n");
 printf("2 Exit the program\n");
 printf("  Please select an option (1 or 2):\n");
 scanf("%i", &selection);

 return(selection);
}

I was under the impression that to open binary files using strings, you could simply create the string, and then implement it as the name of the file where it will read the string. This is what my lecture notes state. However I'm obveously missing something. I've used &name, name, &name[SIZE] within the fopen and each time i've gotten inBinFile == NULL unless I use the commented line. My string is correct. What's wrong? Help is much appreciated. Thanks in advance.

#include <stdio.h>  
#include <stdlib.h> 
#define SIZE 25

int frstmenu(void);  
int sndmenu(void);  

int main()  
{  

    int fmenu, smenu;  
    char name[SIZE];  
    FILE *inBinFile;    
    unsigned char numRead;

  fmenu = frstmenu();
  if ( fmenu !=1 && fmenu !=2 )
  {
        printf("\nIncorrect option\n");
        fmenu = frstmenu();
  }

  if (fmenu == 1)
  {    
       printf("\nEnter the file name: \n");
       scanf("%s", &name[SIZE]);
       /* printf("filename: %s", &name[SIZE]); */

       smenu = sndmenu();

       if (smenu !=1 && smenu !=2 )
       {
           printf("\nIncorrect option\n");
           smenu = sndmenu();
       }
       if (smenu == 1)
       {            

           inBinFile = fopen( name, "rb");
        /* inBinFile = fopen( "stream.grc", "rb"); */

        if (inBinFile == NULL)
        {
          fprintf(stderr, "Error opening %s", &name[SIZE]);
          return(-1);

        fclose(inBinFile);
       }      
  }
  return(0);
}

int frstmenu()
{

  float selection;

  printf("----Menu----\n");
  printf("1 Open a file ( supported format: .grc )\n"); 
  printf("2 Exit the program\n");
  printf("  Please select an option (1 or 2): ");
  scanf("%f", &selection);

  return(selection);

}

int sndmenu()

{

int selection;

 printf("---Menu---\n");
 printf("1 Decode the sequence\n");
 printf("2 Exit the program\n");
 printf("  Please select an option (1 or 2):\n");
 scanf("%i", &selection);

 return(selection);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等往事风中吹 2024-12-08 23:52:33

您可能想说

scanf("%s", &name[0]);

甚至只是:

scanf("%s", name);

您的 &name[SIZE] 指向 name + SIZE超出< /em> 分配的内存。

You probably want to say

scanf("%s", &name[0]);

or even just:

scanf("%s", name);

Your &name[SIZE] points to name + SIZE, which is beyond the allocated memory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文