Thread: Sequential File Reading Error:

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    277

    Unhappy Sequential File Reading Error:

    It complains about an assignment making a pointer to a integer without a cast in the first if statement also complains about a parse error before the first printf. But I think the parse is because I'm testing the file existence the wrong way, all suggestions are welcome.

    Code:
    //Reads data from a sequential file
    
    #include<stdio.h>
    
    int main(){
    	int account;
    	float money;
    	char name[30];
    	FILE *cfPtr;
    	
    	if ((cfPtr = fopen("clients.dat","r") == NULL)
    		printf("Unable to find or open the file")
    	else {
    		printf("%-10s%-13s%s\n", "Account", "Name", "Money");
    		fscanf(cfPtr, "%d%s%f", &account, name, &money);
    		
    		
    		while (!feof(cfPtr)){
    			printf("%-10d%-13s%7.2f\n", account, name, money);
    			fscanf(cfPtr, "%d%s%f", &account, name, &money);
    		}
    	  fclose(cfPtr);
    	}
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Jul 2004
    Posts
    101
    >>if ((cfPtr = fopen("clients.dat","r") == NULL)
    Your parentheses are not balanced. This is what you want.
    Code:
    if ((cfPtr = fopen("clients.dat","r")) == NULL)

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    While you're at it, read this FAQ entry on why you don't want to do this:
    Code:
    while (!feof(cfPtr)){
    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by Princeton
    >>if ((cfPtr = fopen("clients.dat","r") == NULL)
    Your parentheses are not balanced. This is what you want.
    Code:
    if ((cfPtr = fopen("clients.dat","r")) == NULL)
    Thx a lot, well I'm having huge problems using () and {}, specially {} in C, I never know exactly when I should open a {} block, even have been reading about it it seems each style of programming has different options for when using {}.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Maragato
    Thx a lot, well I'm having huge problems using () and {}, specially {} in C, I never know exactly when I should open a {} block, even have been reading about it it seems each style of programming has different options for when using {}.
    You use a braces block { } whenever you want to wrap a few statements together for execution. The most common of these are inside loops, after if statements (and else), and in case statements.
    Here is an example:
    Code:
    if( a == b )
        printf("This statement executes if a == b.\n");
    printf("This always executes.\n");
    Now, if we wanted them both to execute if a == b, we'd wrap them in braces:
    Code:
    if( a == b )
    {
        printf("This statement executes if a == b.\n");
        printf("This does too now.\n");
    }
    Think of each { as saying "Start a group of statements.", and } as "Stop a group of statements.". The same pair is used for for, while and do while loops. Also in switch statements and the like

    You can use them elsewhere, but for now, stick with the basic use of them.

    They're also used in defining structures and unions (and classes in C++), as well as to contain the body of functions, array initializations, and such.

    Parenthesis are used in function calls, definitions, and declarations, as well as in if statements, loops, etc. Another use is as you'd use them in math, to wrap items in parenthesis for determining order of evaluation.

    There's more to it than that, but I'd suggest looking into a good C book for more, as this post would turn into a book were I to show you every example of the usage of both.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I already showed you how to read a file here
    http://cboard.cprogramming.com/showthread.php?t=55688

    So why have yet another go at inventing something different (and still non-functional)

    Code:
    #include<stdio.h>
    
    void do_write ( void ) {
        char buff[BUFSIZ];
        int account;
        char name[30];
        float money;
        FILE *cfPtr;
    
        if ((cfPtr = fopen("client.dat", "w")) == NULL)
            printf("Unable to open the file");
        else {
            printf("Give an account number, a name and the amount of money\n");
            printf("Give EOF to stop\n");
    
            while (fgets(buff, BUFSIZ, stdin) != NULL) {
                if (sscanf(buff, "%d%s%f", &account, name, &money) == 3) {
                    fprintf(cfPtr, "%d %s %.2f\n", account, name, money);
                } else
                    printf("ERROR!");
            }
            fclose(cfPtr);
        }
    }
    
    void do_read ( void ) {
        char buff[BUFSIZ];
        int account;
        char name[30];
        float money;
        FILE *cfPtr;
    
        if ((cfPtr = fopen("client.dat", "r")) == NULL)
            printf("Unable to open the file");
        else {
            while (fgets(buff, BUFSIZ, cfPtr) != NULL) {
                if (sscanf(buff, "%d%s%f", &account, name, &money) == 3) {
                    fprintf(stdout, "%-10d%-13s%7.2f\n", account, name, money);
                } else
                    printf("ERROR!");
            }
            fclose(cfPtr);
        }
    }
    
    
    int main ( void ) {
        do_write();
        do_read();
        return 0;
    }
    Look how similar the writing and reading functions are.
    With a little bit of effort, you could probably pass a few parameters to a single common function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    But the first post was to WRITE in the file the second was to READ... I was just trying to focus 1 doubt per post instead of changing the original subject sorry if I did wrong.
    And quzah thx by the tips, I think I got the line of the idea now.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > But the first post was to WRITE in the file the second was to READ
    One reads from stdin and writes to a file
    One reads from a file and writes to stdout
    It's all copying information from one FILE* to another FILE*
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  2. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  4. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 09:23 PM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM