2011-12-10 24 views
0

下面的问题的主要描述,它发生的地方。但简单地说,我不知道为什么我询问后为什么会收到错误消息努力理解文件指针?

if (outf!=NULL){ 
    printf("Output file already exists, overwrite (y/n):"); 
    scanf("%c",yn); 
} 

其中outf是指向现有文件的文件指针。请在代码的中途阅读说明。

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

int main() { 

/* Declare file pointer */ 
    FILE *inf; 
    FILE *outf; 

    int linenumber,linecounter=0,linepresent; 
    char filename[21]; 
    char detail[21]; 
    char linedetail[21]; 
    char outfilename[21]; 
    char letter,yn='y'; 
    int position; 

/*INPUT DETAILS Ask user for file name and line number*/ 

    printf("Please enter an input filename and a linenumber: "); 
//scan filename to char string and line number to int variable 
    scanf("%s %i",&filename,&linenumber); 

/*OUTPUT DETAILS Ask user for file name, letter & position, etc*/ 
    printf("Please enter an output filename, a letter and a position:");  
    scanf("%s %c %i",&outfilename,&letter,&position); 

/* Open file for reading */ 
    inf=fopen (filename,"r"); 
    outf=fopen(outfilename,"r"); 
/*check that file exists*/ 
    if (inf!=NULL) { 

直到这里一切正常! 然后我试着找出outf文件是否已经存在。如果outf指向一个现有的文件,它会打印“输出文件已经存在,覆盖(y/n):”

但是一旦它打印出来,我就会打开错误窗口!这可能是一个非常新奇的错误 - 我仍然在学习C.如果没有这样的文件,程序正常完成并绕过if语句。

 if (outf!=NULL){ 
      printf("Output file already exists, overwrite (y/n):"); 
      scanf("%c",yn); 
     } 
     if (yn=='y'){ 
    /*keep reading to end of file*/ 
      while (feof(inf)==0) { 
       linecounter++; 
    /*read each line and store the line number in detail[WORDS GO HERE]*/ 
       fscanf (inf,"%s", &detail); 
    /*If we reach the line selected by the user*/ 
       if (linecounter==linenumber){ 
        strcpy(linedetail,detail); 
        linepresent=1; 
       } 
      } 
      if (linepresent==0) { 
       printf("File only contains %i lines",linecounter); 
      } 
     } else { 
      exit(1); 
     } 
    } else { 
     printf("Input file not found"); 
    } 

printf("%s",linedetail); 

/* close the file */ 

    fclose(inf); 
    fclose(outf); 

    return (0); 

} 
+1

不要忘记'&':'scanf(“%c”,&yn)' – pmg

+1

如果你发现有问题的行......你为什么不尝试一个简单的程序,只说'int main ){char yn ='y'; scanf(“%c”,yn);返回0; ''...当这个崩溃的职位是这样吗? : -/*(下次要考虑的事情是,当问题被减少到最小的情况下,最好的情况会产生错误,其他所有事情都会被带出。)* – HostileFork

+0

谢谢,我确实需要改进我的提问技巧!为每个人的耐心欢呼! – user1083734

回答

3

首先,已经提及的问题:你在阅读模式下打开输出文件。要打开它写:

outf=fopen(outfilename,"w"); /* Note the "w". */ 

此外,scanf()的接受指针变量,而不是它们的值,因此,如果你写scanf("%c", yn);,你给scanf函数的指针,这完全是无稽之谈字符y。你需要这样做:scanf("%c", &yn);

但是,即使您修复了这些问题,您的程序也不会达到您的预期。如果您试图打开的文件不存在,fopen()将不会返回NULL,它会创建一个新文件。您的代码将总是覆盖输出文件,如果它存在。返回NULL只有当fopen无法打开/创建文件(例如,您没有权限做),你应该处理这样的:

outf=fopen(outfilename, "w"); 
if(outf == NULL) { 
    perror("Failed to open output file: "); 
    fclose(inf); /* Don't leave files opened. It's bad form. */ 
    exit(1); 
} 
/* Open succeeded, do your stuff here. */ 

注意,不需要else块在if之后,因为exit()立即结束程序。

此外,没有“指向文件的指针”这样的东西。 FILE只是一个表示打开文件的结构。

0

http://www.cplusplus.com/reference/clibrary/cstdio/fopen/

你是开放与读取的标记输出文件。尝试将其更改为“w”。

outf=fopen(outfilename,"w"); 

虽然值得注意的是写入用“w”打开的文件将会敲击旧文件。使用“a”追加到文件。

+0

在我看来,OP打算首先通过以读取模式打开文件来检查文件是否存在。如果返回null,则假定文件不存在。这并不总是正确的,因为缺少读取现有文件的权限也可能返回null。 stat()库函数也许是更好的选择。 – Gowtham

+0

@Gowtham:我认为OP认为打开*输出文件*将返回NULL,如果它不存在。除非他混淆了'if'条件中的变量...... – Staven

+0

@Staven您现在正在尝试检测输出文件是否已经存在。如果确实存在,用户将有选择覆盖或不覆盖。如果它不存在,那么我打算在“写入”模式下创建它。我无法检测它是否存在。我应该怎么做呢? – user1083734

0

您应该将yn的地址传递给scanf函数。

scanf("%c", &yn);