我试图打印一个txt文件,但它没有';我不能用C写作业
我正在编写代码,用来验证.txt文件是否为某种格式 正如我在教程和网站上看到的那样,我编写了我的代码 由于某种原因,我的程序甚至不打印我的文件。 你能告诉我我做错了什么吗? 代码将做一些更复杂的事情,但我仍在尝试我的基础工作 以下是我目前的代码:我试图打印一个txt文件,但它没有';我不能用C写作业,c,file,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
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;
}
}您从文件中读取了两次,但只打印了一次
如果文件太小,第一次读取将读取所有内容,第二次读取将不会产生任何内容,因此您不会打印任何内容。我相信您必须在使用fread后重置指针
尝试fseek(fp,SEEK_SET,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);
}
#包括
#包括
#包括
#包括
//可能在stdio中定义了宏BUFLEN
//哪种尺寸适合分块阅读。
//测试是否可用,否则定义它
#伊夫德夫·布弗伦
#定义BUFLEN 4096
#恩迪夫
int main(int argc,char*argv[])
{
字符源[BUFLEN];
字符*文件名;
文件*fp;
读写尺寸;
字符c;
国际联络处;
如果(argc!=2){
fprintf(stderr,“用法:%s文件名\n”,argv[0]);
退出(退出失败);
}
//重置错误不,以防万一
errno=0;
//复印
filename=malloc(strlen(argv[1])+1);
如果(文件名==NULL){
fprintf(stderr,“分配%zu字节失败\n”,strlen(argv[1])+1);
退出(退出失败);
}
filename=strcpy(filename,argv[1]);
//尝试在“文件名”处打开文件
fp=fopen(文件名,“r”);
如果(fp==NULL){
fprintf(stderr,“打开文件\%s\”文件名失败\n”,文件名);
//errno可能设置为可用的,请检查并打印
如果(错误号!=0){
fprintf(stderr,“错误:%s\n”,strerror(errno));
}
退出(退出失败);
}
//这里有两个选项,一个是读MAXBUFLEN的文章
而((fpread=fread(&source,1,BUFLEN,fp))>0){
//用我们读到的“来源”做点什么
//我们在这里什么都不做,我们只是写信给stdout
writed=fwrite(&source,1,fpread,stdout);
//在写入实际文件时,可以使用“Write”进行错误检查
//但对于stdout来说,这是不可能的(但不是不可能的!)
//测试我们是否写下了我们读到的内容
如果((fpread-WRITED)!=0){
fprintf(stderr,“我们没有写我们读到的内容。差异:%d\n”,
(int)(fpread-write);;
}
}
//fread()无法区分EOF和error,我们必须手动检查
if(feof(fp)){
//我们已经阅读了所有内容,退出
放置(“\n\n\t完成\n”);
//不,等等,我们想用另一种方式再做一次,所以:没有出口
//退出(退出成功);
}否则{
//可能发生了一些错误,请检查
if(费罗(fp)){
fprintf(stderr,“读取\%s\“\n”,文件名时发生了不好的事情”;
如果(错误号!=0){
fprintf(stderr,“错误:%s\n”,strerror(errno));
}
退出(退出失败);
}
}
//另一种方法是逐字节读取
//重置文件指针/错误等。
倒带(fp);
//rewind()应该重置errno,但最好是安全的
errno=0;
printf(“\n\n\tread并再次打印\%s\”\n\n\n”,文件名);
//读取一个字节并打印到文件末尾
而((c=fgetc(fp))!=EOF){
//只需打印。将它们收集到“源”中只是一个练习
fputc(c,stdout);
}
//清理
errno=0;
ret_fclose=fclose(fp);
//甚至fclose()也可能失败
如果(ret_fclose==EOF){
fprintf(stderr,“关闭\%s\“\n”,文件名时发生错误”;
如果(错误号!=0){
fprintf(stderr,“错误:%s\n”,strerror(errno));
}
退出(退出失败);
}
//宏EXIT_FAILURE和EXIT_SUCCESS设置为正确的值
//操作系统告诉它我们是否有eror。
//这里不需要使用exit(),但函数atexit()是存在的
//调用时运行给定函数(例如:清理、安全内容等)的
退出(退出成功);
}
我试着按照格式,希望我能管理好,在我继续之前,我只是想确保我没有写错代码。为什么你要打两次电话给fopen
?没必要那样。也不要返回-1
。在失败时返回一个正值,如1
。你是对的,我修复了这个值,并编辑了我的代码,由于某些原因,我仍然有相同的问题,似乎我丢失了一些东西。你的文本文件有多大?thnx!我做了如下的事情,(我刚刚看到我把我所有的评论都放在了一起),但我仍然无法打印文件,这非常有帮助!thnx!