1

C が初めてで、テキスト置換をその場で行おうとしています。fopen の r+ で読み書きできるようになるはずだと思いました。ファイル内のすべての行を調べて、/ で始まる場合は、行の先頭にhttp://example.comを追加しています。例...行が /tree の場合、その行はhttp://example.com/treeになります。正規表現は正常に機能します。ファイルは正常に読み取れますが、書き込みはできません。理由はありますか?

void
fix_relative (char *page)
{
  FILE *fp;
  fp = fopen ("file", "r+");

  char line[1000];
  regex_t re;
  regcomp (&re, "^/", REG_EXTENDED);

  while (fgets (line, sizeof line, fp) != NULL)
    {
      if (regexec (&re, line, 0, NULL, 0) == 0) {
    fprintf (fp, "http://example.com%s\n", line);
      }
    }

  fclose (fp);
}
4

3 に答える 3

2

This may help you to understand your problem

fopen function

Purpose: Opens a stream. The safer fopen_s function is also available.

Syntax: FILE * fopen(const char *name, const char *mode);

Declared in:

The fopen function opens the file whose name is the string pointed to by name, and associates a stream with it. The string may contain a full path (from the root), a relative path (from the current directory) or just a name.

The argument mode points to a string. If the string is one of the following, the file is open in the indicated mode.

  • "r" Open text file for reading.
  • "w" Truncate to zero length or create text file for writing.
  • "a" Append; open or create text file for writing at end-of-file.
  • "rb" Open binary file for reading.
  • "wb" Truncate to zero length or create binary file for writing.
  • "ab" Append; open or create binary file for writing at end-of-file.
  • "r+" Open text file for update (reading and writing).
  • "w+" Truncate to zero length or create text file for update.
  • "a+" Append; open or create text file for update, writing at end-of-file.
  • "rb+" Open binary file for update (reading and writing).
  • "wb+" Truncate to zero length or create binary file for update.
  • "ab+" Append; open or create binary file for update, writing at end-of-file.
  • "r+b" Same as "rb+"
  • "w+b" Same as "wb+"
  • "a+b" Same as "ab+"

Opening a file with read mode ('r' as the first character in the mode argument) fails if the file does not exist or cannot be read.

Opening a file with append mode ('a' as the first character in the mode argument) causes all subsequent writes to the file to be forced to the then current end-of-file, regardless of intervening calls to the fseek function.

When a file is opened with update mode ('+' as the second or third character in the mode argument), both input and output may be performed on the associated stream. However, output shall not be directly followed by input without an intervening call to the fflush function or to a file positioning function (fseek, fsetpos, or rewind), and input shall not be directly followed by output without an intervening call to a file positioning function, unless the input operation encounters end-of-file.

When opened, a stream is fully buffered if and only of it can be determined not to refer to an interactive device. The error and end-of-file indicators for the stream are cleared.

Returns: A pointer to the object controlling the stream on success, otherwise a null pointer.

于 2013-07-20T14:10:01.523 に答える