reading a string from a file

前端 未结 4 740
Happy的楠姐
Happy的楠姐 2021-01-06 04:03

I have one text file. I have to read one string from the text file. I am using c code. can any body help ?

4条回答
  •  忘掉有多难
    2021-01-06 04:35

    void read_file(char string[60])
    {
      FILE *fp;
      char filename[20];
      printf("File to open: \n", &filename );
      gets(filename);
      fp = fopen(filename, "r");  /* open file for input */
    
      if (fp)  /* If no error occurred while opening file */
      {           /* input the data from the file. */
        fgets(string, 60, fp); /* read the name from the file */
        string[strlen(string)] = '\0';
        printf("The name read from the file is %s.\n", string );
      }
      else          /* If error occurred, display message. */
      {
        printf("An error occurred while opening the file.\n");
      }
      fclose(fp);  /* close the input file */
    }
    

提交回复
热议问题