Thread: Why is this not working?

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    204

    Why is this not working?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct data {
    	int id;
    	char name[30];
    	int number;
    	int grade;
    };
    
    int main(void)
    {
    	FILE *fp;
    	struct data person;
    
    	fp = fopen("person.txt", "r");
    	if (fp = NULL)
    		exit(1);
    
    	fread(&person, sizeof(data), 1, fp);
    
    	fclose(fp);
    
    	printf("%s\n", person.name);
    
    	return 0;
    }
    person.txt
    Code:
    365523
    Max Payne
    28
    82
    When I run this program, the send report window pops up and my program closes. What am I doing wrong? Thanks.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well for starters, it should be:
    Code:
    fread(&person, sizeof(struct data), 1, fp);
    This isn't C++ you know. Unless of course you're compiling incorrectly. Next, you don't have a null on the end of your "string". Oh, and you're probably reading more than your file contains, since this is after all a text file, and you're treating it as a binary fields.


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

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    66
    you have some minor mistakes like
    [CODE]
    if (fp=NULL) //it should be
    if (fp==NULL)//notice the double = symbols
    [\CODE]

    Also I believe that the structure you declare named person of type data should be really a pointer to a structure struct data *person
    try it that way

    what must be happening is that the pointer is trying to acces somewhere in the memory where you should not acces, so it crashes and exis th program because windows is protecting itself

  4. #4
    Registered User
    Join Date
    Aug 2004
    Posts
    66
    sorry, no *person required, its only the double == what is missing

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    204
    Hum, that was silly of me
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct data {
    	int id;
    	char name[30];
    	int number;
    	int grade;
    };
    
    int main(void)
    {
    	FILE *fp;
    	struct data person;
    
    	fp = fopen("person.txt", "r");
    	if (fp == NULL)
    		exit(1);
    
    	fread(&person, sizeof(data), 1, fp);
    
    	fclose(fp);
    
    	printf("%s\n", person.name);
    
    	return 0;
    }
    My output:
    Code:
    23
    Max Payne
    28
    82↕
    Why do I get that extra character after the number 82? Thanks.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If the input is really a text file, then fread() is the wrong thing to be doing - it's not going to convert a string "82" into an integer value 82 for you.

    Also, you have only one printf line, and 4 lines of output - are you really pasting the code you're running?
    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
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Probably. They've probably got it all stuck in the name string, since they're just reading as text, because the entire output fits in the space alotted for 'name'. I'm just surprised it stops there. But no one listens to me any more.


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

  8. #8
    Registered User
    Join Date
    Jul 2005
    Posts
    10
    Quote Originally Posted by louis_mine
    [CODE]if (fp=NULL) //it should be
    if (fp==NULL)//notice the double = symbols
    [\CODE]
    Or you can just do:
    Code:
    if(fp)
    since NULL will evaluate to false...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 08:08 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM