我正在尝试打印一个txt文件,它在C作业中不起作用

时间:2016-08-06 17:08:26

标签: c file

我正在编写应该验证.txt文件是某种格式的代码。

我在教程和网站上看到了我编写的代码 由于某种原因,我的程序甚至不打印我的文件。 你能告诉我我做错了什么吗? 代码会做一些复杂得多的事情,但我仍然在尝试处理我的基础知识。

到目前为止,这是我的代码:

int main(int argc, char *argv[]) {
/* argv[0] = name of my running file
 * argv[1] = the first file that i receive
 */
define MAXBUFLEN 4096

char source[MAXBUFLEN + 1];
int badReturnValue = 1;
char *error = "Error! trying to open the file ";
if (argc != 2) {
    printf("please supply a file \n");
    return badReturnValue;
}
char *fileName = argv[1];


FILE *fp = fopen(argv[1], "r"); /* "r" = open for reading */
if (fp != NULL) {
    size_t newLen = fread(&source, sizeof(char), MAXBUFLEN, fp);
    if (ferror(fp) != 0) {
        printf("%s %s", error, fileName);
        return badReturnValue;
    }
    int symbol;

    while ((symbol = getc(fp)) != EOF) {
        putchar(symbol);


    }
    printf("finish");

    fclose(fp);
}

else {
    printf("%s %s", error, fileName);
    return badReturnValue;
}

}

3 个答案:

答案 0 :(得分:1)

您从文件中读取了两次,但只打印一次

如果文件很小,第一次阅读将读取所有内容,第二次阅读不会产生任何内容,因此您不会打印任何内容。

答案 1 :(得分:0)

我相信你必须在使用fread后重置指针。

尝试使用fseek(fp,SEEK_SET,0)将指针重置为文件的开头。然后打印文件。

答案 2 :(得分:0)

我认为您需要更多解释:

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

// there might be a macro BUFLEN defined in stdio
// which size is optimized for reading in chunks.
// Test if avaiable otherwise define it
#ifndef BUFLEN
#   define BUFLEN 4096
#endif
int main(int argc, char *argv[])
{

  char source[BUFLEN];
  char *filename;
  FILE *fp;
  size_t fpread, written;
  char c;
  int ret_fclose;

  if (argc != 2) {
    fprintf(stderr, "Usage: %s filename\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  // reset errno, just in case
  errno = 0;

  // work on copy
  filename = malloc(strlen(argv[1]) + 1);
  if (filename == NULL) {
    fprintf(stderr, "Allocating %zu bytes failed\n", strlen(argv[1]) + 1);
    exit(EXIT_FAILURE);
  }
  filename = strcpy(filename, argv[1]);

  // try to open the file at 'filename'
  fp = fopen(filename, "r");
  if (fp == NULL) {
    fprintf(stderr, "Opening file \"%s\" filename failed\n", filename);
    // errno might got set to something usable, check and print
    if (errno != 0) {
      fprintf(stderr, "Error: %s\n", strerror(errno));
    }
    exit(EXIT_FAILURE);
  }
  // You have two options here. One is to read in chunks of MAXBUFLEN
  while ((fpread = fread(&source, 1, BUFLEN, fp)) > 0) {
    // Do something with the stuff we read into "source"
    // we do nothing with it here, we just write to stdout
    written = fwrite(&source, 1, fpread, stdout);
    // you can use 'written' for error check when writing to an actual file
    // but it is unlikely (but not impossible!) with stdout

    // test if we wrote what we read
    if ((fpread - written) != 0) {
      fprintf(stderr, "We did not write what we read. Diff: %d\n",
          (int) (fpread - written));
    }
  }
  // fread() does not distinguish between EOF and error, we have to check by hand
  if (feof(fp)) {
    // we have read all, exit
    puts("\n\n\tfinish\n");
    // No, wait, we want to do it again in a different way, so: no exit
    // exit(EXIT_SUCCESS);
  } else {
    // some error may have occured, check
    if (ferror(fp)) {
      fprintf(stderr, "Something bad happend while reading \"%s\"\n", filename);
      if (errno != 0) {
    fprintf(stderr, "Error: %s\n", strerror(errno));
      }
      exit(EXIT_FAILURE);
    }
  }

  // the other way is to read it byte by byte

  // reset the filepointers/errors et al.
  rewind(fp);
  // rewind() should have reseted errno, but better be safe than sorry
  errno = 0;
  printf("\n\n\tread and print \"%s\" again\n\n\n\n", filename);

  // read one byte and print it until end of file
  while ((c = fgetc(fp)) != EOF) {
    // just print. Gathering them into "source" is left as an exercise
    fputc(c, stdout);
  }

  // clean up
  errno = 0;
  ret_fclose = fclose(fp);
  // even fclose() might fail
  if (ret_fclose == EOF) {
    fprintf(stderr, "Something bad happend while closing \"%s\"\n", filename);
    if (errno != 0) {
      fprintf(stderr, "Error: %s\n", strerror(errno));
    }
    exit(EXIT_FAILURE);
  }
  // The macros EXIT_FAILURE and EXIT_SUCCESS are set to the correct values for
  // the OS to tell it if we had an eror or not.

  // Using exit() is noot necessary here but there exits teh function atexit()
  // that runs a given function (e.g: clean up, safe content etc.) when called
  exit(EXIT_SUCCESS);
}