Calling an already Open File in a Function?

i have a bunch of functions, and when they are called to, it has to read from an already opened file that the user has specified when the program loads (code below) can someone help me and tell me how id go about having it read the file in the function called? (one of my function codes is also below. (where it says enter a number, i want it to read from the file. any help would be appreciated.)

Open File Code:
[code]printf("
Enter the filename & extension you'd like to open
");
printf("ex: filename.txt
");
scanf("%49s", filename);
if ( ( filePtr = fopen( filename, "r" ) ) == NULL )
printf("ERROR READING FILE: %s Could Not Be Opened! Please Try Again!
", filename);
else {
printf("File Successfully Opened! Please Choose A Conversion.
");
}
menu();[/code]

Decimal to Hex Function:
[code]void dectohex(void) /*Function prototype*/
{
unsigned i; /*declare variables*/
unsigned x;

printf("Enter a number: "); /*prompt user to enter a #*/
scanf("%d",&i); /*store the number in unsigned i*/

printf("Hex # is: %x
",i); /*converts decimal to hexadecimal*/
menu();
} [/code]

Comments

  • : i have a bunch of functions, and when they are called to, it has to read from an already opened file that the user has specified when the program loads (code below) can someone help me and tell me how id go about having it read the file in the function called? (one of my function codes is also below. (where it says enter a number, i want it to read from the file. any help would be appreciated.)
    :
    : Open File Code:
    : [code]printf("
    Enter the filename & extension you'd like to open
    ");
    : printf("ex: filename.txt
    ");
    : scanf("%49s", filename);
    : if ( ( filePtr = fopen( filename, "r" ) ) == NULL )
    : printf("ERROR READING FILE: %s Could Not Be Opened! Please Try Again!
    ", filename);
    : else {
    : printf("File Successfully Opened! Please Choose A Conversion.
    ");
    : }
    : menu();[/code]
    :
    : Decimal to Hex Function:
    : [code]void dectohex(void) /*Function prototype*/
    : {
    : unsigned i; /*declare variables*/
    : unsigned x;
    :
    : printf("Enter a number: "); /*prompt user to enter a #*/
    : scanf("%d",&i); /*store the number in unsigned i*/
    :
    : printf("Hex # is: %x
    ",i); /*converts decimal to hexadecimal*/
    : menu();
    : } [/code]
    :

    Yeah, if you want to work on a file you need to use file handling functions. The C standard library has functions like fprintf. You simply pass the file handle you got from fopen() as the first argument and treat them like normal. Check your documentation and/or textbook. Alternatively, you could use standard input/ouput redirection and write the code as if it all came from the console, which if the IO isn't redirected it will. Then you'd run the program: myprog <file

    where myprog is the name of the executable and file is the name of the file you want to read. Obviously if you do this you will not need to ask for a filename in your program (at least to read from and assuming you don't change the file you are reading from). If you don't specify the input redirection then the program will run and take keyboard input as the data from the file.

    "We can't do nothing and think someone else will make it right."
    -Kyoto Now, Bad Religion

  • would you be able to give me an example of how id do that? ive tried a few things w/fprintf and have yet to be successful. also would i have to use: rewind (filename) somewhere within it to always start at the beginning? this whole part with the file readin is still pretty new to me and the book i have doesnt do to well at explaining everything.
  • : would you be able to give me an example of how id do that? ive tried a few things w/fprintf and have yet to be successful. also would i have to use: rewind (filename) somewhere within it to always start at the beginning? this whole part with the file readin is still pretty new to me and the book i have doesnt do to well at explaining everything.
    :

    Well a quick example of using the fprintf (and from this you can figure how fscanf etc. work)

    fprintf(filePtr,"Hello World %d
    ",5);

    That would write "Hello World 5" (minus the quotes) to whatever file filePtr points. filePtr being the FILE * (handle) that you got from fopen. As you can see, it works just like printf() only with a FILE * as the first argument, in fact, fprintf(stdout,"whatever") will do exactly what printf("whatever") does.

    You would need to use rewind or fseek to get back to the beginning of a file, but why would you want to do that? Unless you are working with a massive amount of data (meaning you'd probably be using a database system, or should be) you typically want to read all the data in at once and manipulate it in memory, then write all the data out (if you do) at once. For example, when you write something in a wordprocessing program it doesn't write whatever you type into the file until you tell it to save the file. You'd have to go into more detail about what your program is supposed to do for me to tell you whether rewind would be a good idea (read: simplest, but not the most efficient solution).

    "We can't do nothing and think someone else will make it right."
    -Kyoto Now, Bad Religion

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion