File i/o and ASCII question

This is a discussion on File i/o and ASCII question within the C Programming forums, part of the General Programming Boards category; This is an incomplete program, but what I have now is main calling openFiles, which is supposed to check if ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    33

    File i/o and ASCII question

    This is an incomplete program, but what I have now is main calling openFiles, which is supposed to check if this file is successfully opened for writing and reading. I'm getting these errors which I haven't figured out:

    18: warning: passing arg 1 of `openFiles' from incompatible pointer type
    18: warning: passing arg 2 of `openFiles' from incompatible pointer type
    In function `openFiles':
    35: error: syntax error before ')' token


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <stdbool.h>
    
    
    bool openFiles(FILE **pFPIn, FILE **pFPOut, int* input1, int* input2);
    
    
    
    
    
    int main(void)
    {
    FILE *fpIn, *fpOut;
    int input1, input2;
    
    openFiles(fpIn, fpOut, &input1, &input2);                         //line 18
    generate(input1, input2);
    
    return 0;
    }
    
    
    
    bool openFiles(FILE **pFPIn, FILE **pFPOut, int* input1, int* input2)
    {
       FILE *fpIn, *fpOut;
       bool success = false;
    
       if ((fpIn = fopen("test.txt", "r")) != NULL)
       {
          if ((fpOut = fopen("test.out", "w")) != NULL)
          {
             fscanf(fpIn, "%d%d", &input1, &input2) == 2)         //line 35
             fclose(fpOut);  
             success = true;
          }  
          else
             printf("\nUnable to open \"test.out\" for writing.\n\n");
          fclose(fpIn);
       }
       else
          printf("\nUnable to open \"test.txt\" for reading.\n\n");
       
       return success;
    }


    I also plan to pseudo randomly generate numbers into a text file. How would I get these numbers to correspond to ASCII characters when they are put into the text file?

    Thanks.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    compilers are actually incredibly "smart". listen to them, they arent lying.

    line 18: compiler says there is something wrong with both arg 1 and arg 2 of your openFiles function. the function takes FILE** as both arg1 and arg2, but you are passing only FILE* for arg1 and arg2 (consider using &, as you did with the int arguments).

    line 35: read this exact line character by character and see what is missing. a similar error would be caused by the following (can you see it?)
    Code:
    int x
    edit:
    How would I get these numbers to correspond to ASCII characters when they are put into the text file?
    not sure i understand. you basically want to generate random characters? it would be the same as generating a random number (1-26) and adding the ASCII value for the first character you start at (ie 'a' is 65 and 'A' is 97). NB: i doubt my ascii values are correct, but are very close, do a simple search for the ascii chart. after generating this value, say 100, you can cast or print it as a character.
    Last edited by nadroj; 11-04-2008 at 09:38 PM.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    33
    Quote Originally Posted by nadroj View Post

    line 18: compiler says there is something wrong with both arg 1 and arg 2 of your openFiles function. the function takes FILE** as both arg1 and arg2, but you are passing only FILE* for arg1 and arg2 (consider using &, as you did with the int arguments).
    Thanks, I actually tried that earlier and it didn't work (I guess I had something wrong elsewhere at the time) so I didn't think to try it again

    Quote Originally Posted by nadroj View Post
    line 35: read this exact line character by character and see what is missing. a similar error would be caused by the following (can you see it?)
    Code:
    int x
    Missing a semicolon? I'm not sure if thats right, but I tried adding a closing semicolon and it made no difference

    Quote Originally Posted by nadroj View Post
    edit: not sure i understand. you basically want to generate random characters? it would be the same as generating a random number (1-26) and adding the ASCII value for the first character you start at (ie 'a' is 65 and 'A' is 97). NB: i doubt my ascii values are correct, but are very close, do a simple search for the ascii chart. after generating this value, say 100, you can cast or print it as a character.
    What I'm trying to do is seed a random number generator to a range (say [10,20]) , then have it print n amount of random numbers into a text file, but have them be printed as their ASCII equivalents

    I hope that was clear. is that your same impression of it? If it is, then i'll try to do what you suggested

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Quote Originally Posted by muzihc View Post
    Missing a semicolon? I'm not sure if thats right, but I tried adding a closing semicolon and it made no difference
    "closing" semicolon? there is only 1 semicolon (no "open" and "close"). but that line definitely needs a semicolon. i cant spot any other error. can you post the updated code that your compiling again?

    What I'm trying to do is seed a random number generator to a range (say [10,20]) , then have it print n amount of random numbers into a text file, but have them be printed as their ASCII equivalents
    I hope that was clear. is that your same impression of it? If it is, then i'll try to do what you suggested
    i see, thats easier than only printing characters. basically you have a for loop (counter goes from 0 to "n"). each iteration in the loop you generate a number (int) in the given range, which if you dont know how to do, do a search here or anywhere to find out. you can then simply write the integer to the file as a character ("&#37;c"). note that not every character in ASCII (ie 1-127 or 1-254 for extended ASCII) is printable, see the chart.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Looks like Line 18 is about the difference in the type of the function arguments in the caller and the callee as well as in the prototype. Pass one instead of two levels of indirection in the callee and the prototype ie
    Code:
    bool openFiles(FILE *pFPIn, FILE *pFPOut, int *input1, int *input2);
    IMHO though what's the need of passing all the arguments to openFiles() when you aren't even using them there. All the parameters can simply be local variables.

    Line 35 looks like part of an if-else statement. Its missing the if keyword and a parenthesis as in.
    Code:
    if (fscanf(fpIn, "%d%d", &input1, &input2) == 2)

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    33
    (sorry for delay, im doing like 3 things at once)

    Quote Originally Posted by nadroj View Post
    "closing" semicolon? there is only 1 semicolon (no "open" and "close"). but that line definitely needs a semicolon. i cant spot any other error. can you post the updated code that your compiling again?
    Here's my updated code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <stdbool.h>
     
    
    bool openFiles(FILE **pFPIn, FILE **pFPOut, int* input1, int* input2);
    
    
     
       
       
    int main(void)
    {
    FILE *fpIn, *fpOut;
    int input1, input2;
           
    openFiles(&fpIn, &fpOut, &input1, &input2);
    generate(input1, input2);
             
    return 0;
    }
             
          
        
    bool openFiles(FILE **pFPIn, FILE **pFPOut, int* input1, int* input2)
    {
       FILE *fpIn, *fpOut;
       bool success = false;
     
       if ((fpIn = fopen("test.txt", "r")) != NULL)
       {
          if ((fpOut = fopen("test.out", "w")) != NULL)
          {
             fscanf(fpIn, "%d%d", &input1, &input2) == 2);
             fclose(fpOut);  
             success = true;
          }  
          else
             printf("\nUnable to open \"test.out\" for writing.\n\n");
          fclose(fpIn);
       }
       else
          printf("\nUnable to open \"test.txt\" for reading.\n\n");
       
       return success;
    }
    i see, thats easier than only printing characters. basically you have a for loop (counter goes from 0 to "n"). each iteration in the loop you generate a number (int) in the given range, which if you dont know how to do, do a search here or anywhere to find out. you can then simply write the integer to the file as a character ("%c"). note that not every character in ASCII (ie 1-127 or 1-254 for extended ASCII) is printable, see the chart.
    Thanks, I see what you're saying but I might have trouble correctly writing that code (I'll post if I do)

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    33
    Quote Originally Posted by itCbitC View Post
    Looks like Line 18 is about the difference in the type of the function arguments in the caller and the callee as well as in the prototype. Pass one instead of two levels of indirection in the callee and the prototype ie
    Code:
    bool openFiles(FILE *pFPIn, FILE *pFPOut, int *input1, int *input2);
    IMHO though what's the need of passing all the arguments to openFiles() when you aren't even using them there. All the parameters can simply be local variables.
    Well I can take out the last two args, but the first two have to be there
    Code:
      bool openFiles(FILE **pFPIn, FILE **pFPOut);
    my teacher requires that I use that prototype


    Quote Originally Posted by itCbitC View Post
    Line 35 looks like part of an if-else statement. Its missing the if keyword and a parenthesis as in.
    Code:
    if (fscanf(fpIn, "%d%d", &input1, &input2) == 2)
    Thanks. Since it's not supposed to be an if-else statement I removed the end and it works properly now

    Code:
     fscanf(fpIn, "%d%d", &input1, &input2);

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    So it is required of you to use two levels of indirection where one suffices??

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by itCbitC View Post
    So it is required of you to use two levels of indirection where one suffices??
    If you want to change the value of the pointer passed in (say, by opening a file, which seems silly in a function called openFiles, but there you go) you would need to pass a pointer to the pointer.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    33
    Quote Originally Posted by itCbitC View Post
    So it is required of you to use two levels of indirection where one suffices??
    Are you saying that one suffices in my function, or in general? If it's the former, than my function will need to be changed. (or my teacher made a typo) :|

  11. #11
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Quote Originally Posted by tabstop View Post
    If you want to change the value of the pointer passed in (say, by opening a file, which seems silly in a function called openFiles, but there you go) you would need to pass a pointer to the pointer.
    Your point is correct, however openFiles() isn't using or changing them in any way as it's using two local FILE * pointers.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by itCbitC View Post
    Your point is correct, however openFiles() isn't using or changing them in any way as it's using two local FILE * pointers.
    Which is the problem.

  13. #13
    Registered User
    Join Date
    Feb 2005
    Posts
    38
    Hey muzihc, I can tell that you're in my same class. Gaudry definitely isn't the best teacher around, in fact, she's probably one of the worst! I really feel bad for all the people who don't have any programming experience that are taking her. She really likes us to over modularize our programs which, in my opinion, is quite stupid. She makes programs that take about 50 lines to code and turns them into about 200 lines, it's retarded.

    Anyways, I see a few problems with your code. First of all, you should probably have a different function for reading in the two integers from the text file, as Gaudry specifically stated what she wanted the function prototype to be (the two file pointers only).

    Second, it's not necessary to have 2 more file pointers "fpOut" and "fpIn" declared in openFiles() as you've already declared the ones you're going to be using in main(). I'd change the names in main to "pFPIn" and "pFPOut" as to avoid any confusion.

    The code you posted the second time around doesn't actually compile because of your fscanf() statement in openFiles(). I'm sure you can see why.

    Also, when you remove the redundant file pointers in openFiles() you're going to use the ones you declared in the functions arguments. When you try to open the files though, you're going to need to dereference pFPIn and pFPOut to get them correctly assigned.
    I like to play pocket pool.

  14. #14
    Registered User
    Join Date
    Oct 2008
    Posts
    33
    Quote Originally Posted by NoUse View Post
    Hey muzihc, I can tell that you're in my same class. Gaudry definitely isn't the best teacher around, in fact, she's probably one of the worst! I really feel bad for all the people who don't have any programming experience that are taking her. She really likes us to over modularize our programs which, in my opinion, is quite stupid. She makes programs that take about 50 lines to code and turns them into about 200 lines, it's retarded.
    Glad to see you here. I agree about Gaudry 100%, I wonder if there is a support group out there haha. Anyways, I just got out all my stuff to work on calculus, so I'm gonna get back to this in the morning.

    I read through all your help though and I see exactly what you're saying.

    Also, when you remove the redundant file pointers in openFiles() you're going to use the ones you declared in the functions arguments. When you try to open the files though, you're going to need to dereference pFPIn and pFPOut to get them correctly assigned.
    I initially had some trouble visualizing this, but now that I think about it I get it.

    Thanks for all the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 05:27 PM
  2. File I/O both ASCII and binary handling in C++
    By random in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2005, 05:53 AM
  3. Encrypting text file with ASCII hex
    By supaben34 in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2005, 06:35 PM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 11:51 PM
  5. Binary File I/O
    By sean in forum C Programming
    Replies: 7
    Last Post: 08-20-2004, 11:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21