C文件读取循环总是打破

时间:2014-04-16 11:07:34

标签: c fopen eof

一直在寻找C和我正在研究一个简单的程序来读取文本文件,应用凯撒密码,并写入新的输出文件。我的问题是,应该构建我的输出字符串的while循环立即终止,声称下一个字符是EOF,即使它显然不是。代码如下:

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

#define MORD ".mord"

void die(const char *message)
{
  if(errno) {
    perror(message);
  } else {
    printf("ERROR: %s\n", message);
  }

  exit(1);
}

int main(int argc, char *argv[])
{
  if(argc != 3) die("USAGE: dodsmord <filename> <offset>");
  char *infilename = argv[1];
  char *outfilename = strcat(infilename, MORD);
  int offset = atoi(argv[2]);
  char *outstr[1000];
  FILE *infile = fopen(infilename, "r+");
  FILE *outfile = fopen(outfilename, "w+");
  if(infile == NULL) {
    die("Could not open input file");
  }
  if(outfile == NULL) {
    die("Could not open output file");
  }
  int c;
  int i = 0;
  printf("reading input file...\n");
  while(1) {
    c = fgetc(infile);
    printf("c == EOF: %d\n", c == EOF ? 1 : 0);
    printf("EOF: %d\n", EOF);
    printf("c is now: %c\n", c);
    if(c == EOF) break;
    outstr[i] = c + offset;
    i++;
  }
  printf("done reading! writing outstr to outfile...\n");
  fwrite(outstr, sizeof(char), i, outfile);

  printf("closing streams...\n");
  fclose(infile);
  fclose(outfile);

  return 0;
}

输出如果我在test.txt上运行代码(其中包含&#34; abcdefg&#34;没有引号),我得到输出

reading input file...
c == EOF: 1
EOF: -1
c is now: �
done reading! writing outstr to outfile...
closing streams...

4 个答案:

答案 0 :(得分:9)

char *infilename = argv[1];
char *outfilename = strcat(infilename, MORD);

这不符合你的想法。

设置infilename以便它指向与相同的内存,然后strcat更改该内存以附加.mord

然后它返回它,以便outfilename 指向内存。

因此,您将输入文件名更改为.mord,并且当您尝试打开它时,我不知道究竟会发生什么,这取决于是否它还存在。

你想要的是:

char *infilename = argv[1];
char *outfilename = malloc (strlen (infilename) + strlen (MORD) + 1);
if (outfilename == NULL) {
    handleOutOfMemoryHere();
}
strcpy (outfilename, infilename);
strcat (outfilename, MORD);
:
// weave you cypher magic here
:
free (outfilename);

该代码中的第二行将为输出文件名提供单独的内存区域,strcpy/strcat组合将正确构建它。

答案 1 :(得分:2)

问题在于:

  char *infilename = argv[1];
  char *outfilename = strcat(infilename, MORD);

strcat()将更改infilename,并将MORD附加到其末尾,因此您使用argv[1].mord作为输入文件名。我打赌这是一个空文件。

解决方案:

更改

char *outfilename = strcat(infilename, MORD);

char *outfilename = malloc(strlen(infilename) + strlen(MORD) + 1);
if (outfilename == NULL) { /* malloc failed */ }
strcpy(outfilename, infilename);
strcat(outfilename, MORD);

答案 2 :(得分:1)

问题是由以下原因造成的:

char *infilename = argv[1];
char *outfilename = strcat(infilename, MORD);

原因:strcat执行就地复制并返回src的地址。试试这个,你会看到:

char *infilename = argv[1];
char *outfilename = strcat(infilename, MORD);
printf("TEST: %p %p\n", infilename, outfilename);

要解决此问题,请使用以下内容:

char *infilename = argv[1];
char *outfilename = malloc(strlen(infilename) + strlen(MORD) + 1);
strcpy(outfilenname, infilename);
strcat(outfilename, MORD);

答案 3 :(得分:1)

问题在于以下行

char *outfilename = strcat(infilename, MORD);

infilename被修改为test.txt.mord

移动

FILE *infile = fopen(infilename, "r+");

以上一行

    char *outfilename = strcat(infilename, MORD);