简体   繁体   English

从c中的文件读取字符串

[英]reading strings from a file in c

I am reading a string from a file in C . 我正在从C的文件读取字符串。 The string is supposed to have a specific length and start with thisisnumbr . 该字符串应具有特定长度,并以thisisnumbr If both requirements are met, then something else is supposed to happen. 如果同时满足这两个要求,那么应该发生其他事情。 Furthermore, I want to prevent that anything unexpected in the file might cause a crash. 此外,我想防止文件中任何意外内容都可能导致崩溃。

My code: 我的代码:

#define MYSTRING "thisisnumb-"

void read_mystring()
{
  int c, i = 0, len =0 ;
  char input[sizeof( MYSTRING)+2] ;
  char check[] =  MYSTRING ;
  FILE *file ;
  file = fopen("/pathto/myfile", "r") ;
  if (file) {
      while ((c = getc(file)) != EOF)
      {
          input[i] = c ;
          i++ ;
          if (i > sizeof(input))
          {      
            len = 1 ;
            break ;
          }
      }
      fclose(file) ;
  }
  if(strncmp(input,check,sizeof(check)-1) == 0  && len == 0)
  {
   //do something
  }
}

So input has the size of MYSTRING plus 2 more characters (supposed to be 2 digits. 因此, input的大小为MYSTRING另加2个字符(假定为2位数字)。

In the while loop I am reading myfile and storing it in input . while循环中,我正在读取myfile并将其存储在input With

if (i > sizeof(input))
{      
   len = 1 ;
   break ;
}

I make sure that the string reading stops if the string in the file appears to be longer than expected. 如果文件中的字符串看起来比预期的长,我确保字符串读取停止。

Then I compare the beginning of the string with strncmp and check if len==0 to make sure the string starts with MYSTRING AND also has the correct length. 然后,我将字符串的开头与strncmp进行比较,并检查len==0是否确保字符串以MYSTRING开头并且长度也正确。

If so, something else happens. 如果是这样,还会发生其他情况。

This works, meaning that I don't get an Segmentation fault if there is no file, the string in file is too long, or the string in the file doesn't start with MYSTRING . 这行得通,这意味着如果没有文件,文件中的字符串太长或文件中的字符串不是以MYSTRING开头,则不会出现MYSTRING

I was wondering, if there is anything else that might break my program? 我在想,是否还有其他事情可能会破坏我的程序?

And also, when I do printf("input=%s\\n",input) at the end of my function, I get my string but also an additional line with garbage? 而且,当我在printf("input=%s\\n",input)末尾执行printf("input=%s\\n",input)时,我得到了我的字符串,而且还有一行带有垃圾的内容?

Any ideas? 有任何想法吗?

There are a number of things you need to look at. 您需要看很多东西。 Foremost sizeof MYSTRING includes the storage size required for the nul-byte . sizeof MYSTRING最主要sizeof MYSTRING包括nul-byte所需的存储大小。 It is strlen + 1 . 它是strlen + 1 You must be very careful mixing sizeof string (on a char array) and string length . 您必须非常小心地将sizeof string (在char数组上)和string length混合使用

Next, if you call this function more than once throughout your code, it may be better to fopen the file in the caller and pass a FILE* parameter to your function. 接下来,如果您在整个代码中多次调用此函数,则最好在调用程序中fopen文件,然后将FILE*参数传递给函数。 (it's up to you) I would do: (由您决定)我会做:

/* open file in caller to prevent repeatedly opening and closing file */
FILE *fp = fopen (fname, "r");

if (!fp) {  /* validate file open for reading */
    fprintf (stderr, "error: file open failed '%s'.\n", fname);
    exit (EXIT_FAILURE);
}

Next, there are many ways to handle your function itself. 接下来,有很多方法可以处理您的函数本身。 As mentioned in the comment, you are better served by providing a buffer large enough to handle long strings in the file (and even then you need to validate a complete line read occurred) Reading with fgets the '\\n' is read and included in the resulting buffer, so you will need to remove the trailing '\\n' by overwriting with a nul-byte , eg 如评论中所述,最好提供一个足够大的缓冲区来处理文件中的长字符串(即使这样,您仍需要验证是否发生了完整的行读取),才能更好地为您服务。用fgets读取并读入'\\n'结果缓冲区,因此您需要通过用nul-byte覆盖来删除结尾的'\\n' ,例如

    char buf[BUFSZ] = "";
    while (fgets (buf, BUFSZ, fp)) {
        size_t len = strlen (buf);
        if (len > 0 && buf[len - 1] == '\n')
            buf[--len] = 0;
        else {
            /* handle more chars remain in line than buf can hold */
        }

After validating your line read, you simply need to check the length against your requirement and then check that the last two characters are digits , eg 验证行读取之后,您只需要根据需要检查长度,然后检查最后两个字符是否为数字 ,例如

        if (len != sizeof MYSTRING + 1) {
            /* not right length - handle error */
        }

        if (strncmp (buf, MYSTRING, sizeof MYSTRING - 1) == 0 &&
            isdigit (buf[sizeof MYSTRING - 1]) &&
            isdigit (buf[sizeof MYSTRING]))
        {
            /* string matches criteria -- do something */
        }
        else {
            /* doesn't meet conditon -- handle error */
        }

Putting it altogether, and adding a moretoread flag to read until the end of a long line if it exceeds BUFSZ , you would have something similar to the following: 放在BUFSZ ,如果超过BUFSZ ,则添加一个moretoread标志以读取直到长行的BUFSZ ,您将具有类似于以下内容:

void read_mystring (FILE *fp)
{
    char buf[BUFSZ] = "";
    int moretoread = 0;

    while (fgets (buf, BUFSZ, fp)) {
        size_t len = strlen (buf);
        if (len > 0 && buf[len - 1] == '\n') { /* check for newline */
            buf[--len] = 0;                    /* overwrite with nul-byte */
            moretoread = 0;                    /* reset moretoread flag */
        }
        else {
            /* handle more chars remain in line than buf can hold */
            moretoread = 1;
        }
        if (moretoread)    /* you are way over your wanted length */
            continue;      /* just read until newline encountered */
        if (len != sizeof MYSTRING + 1) {
            /* not right length - handle error */
        }

        /* check prefix followed by two digits */
        if (strncmp (buf, MYSTRING, sizeof MYSTRING - 1) == 0 &&
            isdigit (buf[sizeof MYSTRING - 1]) &&
            isdigit (buf[sizeof MYSTRING]))
        {
            /* string matches criteria -- do something */
        }
        else {
            /* doesn't meet conditon -- handle error */
        }
    }
}

Include ctype.h for isdigit() . isdigit()包括ctype.h

Like I said, there are many, many different approaches you can take, these are just thoughts based on your conditions and one way of doing it. 就像我说的,您可以采用许多不同的方法,这些只是基于您的条件和一种实现方式的想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM