c - How do I use pointers in combination with getc? -


i have function getnum(), gets number file , returns it. when go getnum() have lost pointer , starts @ begging of file again. i'm wondering how location of getc , go place. couldn't find how in 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); 2 = 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); } 

the reason reopen file each time execute getnum. when open file reading, starts @ start of file. instead open 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 before, no file opening. } 

Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -