如何将指针与 getc 结合使用?

发布于 2024-09-25 14:27:57 字数 794 浏览 2 评论 0原文

我有一个函数 getNum(),它从文件中获取一个数字并返回它。当我返回 getNum() 时,我丢失了指针,它再次从文件的请求处开始。我想知道如何获取 getc 所在的位置,然后返回到那个地方。我在手册或论坛中找不到如何执行此操作。谢谢。

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

int getNum();
int getLine();
int getMatrix();



main() {
int num;
int two;
num = getNum();
printf("%d\n", num);
two = getNum();
printf("%d\n", two);

}

int getNum() {
  FILE *infile;
    infile = fopen("matrix.txt","r");
  int c;
  double value = 0;

  while ((c=getc(infile)) != '\n') {
    if(c==32){
      if(value != 0){
        return(value);
      }
      //otherwise keep getting characters
    }
    else if ((c<=47)||(c>=58)){
      printf("incorrect number input %d\n", c);
      exit(1);
    }
    else {
      value = (10*value) + c - '0';
    }
  }
  return(value);
}

I have a function getNum(), which gets a number from file and returns it. When I go back into getNum() I have lost the pointer and it starts at the begging of the file again. I'm wondering How do I get the location of where getc is and then go back to that place. I couldn't find how to do this in the manual or in forums. Thank you.

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

int getNum();
int getLine();
int getMatrix();



main() {
int num;
int two;
num = getNum();
printf("%d\n", num);
two = getNum();
printf("%d\n", two);

}

int getNum() {
  FILE *infile;
    infile = fopen("matrix.txt","r");
  int c;
  double value = 0;

  while ((c=getc(infile)) != '\n') {
    if(c==32){
      if(value != 0){
        return(value);
      }
      //otherwise keep getting characters
    }
    else if ((c<=47)||(c>=58)){
      printf("incorrect number input %d\n", c);
      exit(1);
    }
    else {
      value = (10*value) + c - '0';
    }
  }
  return(value);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

静待花开 2024-10-02 14:27:57

原因是每次执行 getNum 时都会重新打开该文件。当您打开文件进行读取时,它将从文件的开头开始。相反,只需打开一次。

int main(int argc, char *argv[])
{
  ...
  FILE *infile;
  ...
  infile = fopen("matrix.txt","r");
  ...
  getNum(infile)
  ...
  fclose(infile);
  return 0;
}


int getNum(File *infile)
{
  // Same as before, just no file opening.
}

The reason is that you reopen the file each time you execute getNum. When you open a file for reading, it starts at the start of the file. Instead open it just once.

int main(int argc, char *argv[])
{
  ...
  FILE *infile;
  ...
  infile = fopen("matrix.txt","r");
  ...
  getNum(infile)
  ...
  fclose(infile);
  return 0;
}


int getNum(File *infile)
{
  // Same as before, just no file opening.
}
ゃ人海孤独症 2024-10-02 14:27:57

每次调用 getNum 时都会重新打开该文件,因此您自然会回到开始处。

相反,在主程序中打开文件并将 FILE * 传递给 getNum()。

You re-open the File each time you Call getNum, so naturally you are back at the start.

Instead open the file in your main and pass the FILE * to getNum().

妞丶爷亲个 2024-10-02 14:27:57

每次调用函数时都会重新打开文件。新打开的文件从头开始扫描。

另一种方法是在 getNum() 之外打开文件一次。您可以将 FILE* 作为参数传递给 getNum()。

另外,您没有关闭该文件。在所有调用 getNum() 后,使用 fclose() 关闭文件。

You're opening the file anew with each function call. The newly opened file begins scanning from the beginning.

An alternative would be to open the file once, outside getNum(). You could pass the FILE* to getNum() as an argument.

Also, you're not closing the file. Use fclose() to close the file after all the calls to getNum().

小傻瓜 2024-10-02 14:27:57

像这样的东西:

int getNum( FILE* fp );
...

int n;
FILE* file = fopen( "file.txt" );
assert( file );
do {
    n = getNum( file );
    /* ... */
}
while ( n != -1 ); /* or something */
fclose( file );

Something like:

int getNum( FILE* fp );
...

int n;
FILE* file = fopen( "file.txt" );
assert( file );
do {
    n = getNum( file );
    /* ... */
}
while ( n != -1 ); /* or something */
fclose( file );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文