Thread: I/O problem

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    49

    I/O problem

    Hey everyone! Been a while since I have been on the board, but so far I have had no problems..lol Well this semesterI am learning C++ and ran into a bit of trouble.

    The program is supposed to read from the supplied file and then count how many files are present. The problem is though that everytime I run it I get a coredump.

    Any advice?

    Code:
    #include<iostream.h>
    #include<stdio.h>
    
    #define NAME_LEN 50
    
    typedef struct nodetag{
    	int recordNum;
    	char name[NAME_LEN+1];
    	int quantity;
    	float unitPrice;
    }Hardware;
    
    FILE *fp;
    
    int count(void);
    char menu(void);
    
    int main()
    {
    	system("clear");
    
    	int count2=0, c=0;
    
        if((fp = fopen("hardware.dat", "r")) == NULL)
    		cout<< "Error to open the file";
    
    	else
    	{
    
    	cout<< "\n\tTotal counts: " << count();
    
    	}
    
    	return 0;
    }
    
    int count(void)
    {
    	int c=0;
    	int recordNum, quantity;
    	char name;
    	float unitPrice;
    
    	fscanf(fp, "%d%s%d%f", &recordNum, name, &quantity, &unitPrice);
    
    	while(!feof(fp))
    	{
    		c++;
    		fscanf(fp, "%d%s%d%f", &recordNum, name, &quantity, &unitPrice);
    	}
    
    	return c;
    }
    "If it is too loud, your too old!" -unknown
    "You mean Honda Civics are not the almightiest of cars?" -My friend


    One of the funniest things I have ever heard:
    Guy at car show: "Damn that thing must do wicked burn outs."
    Me: "That is a STI! It is All Wheel Drive!"
    Guy: "Yeah...so?"

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >int count(void)
    >{
    >	int c=0;
    >	int recordNum, quantity;
    >	char name;
    It looks like you wanted:
    char name[NAME_LEN+1];

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    Originally posted by swoopy
    Code:
    >int count(void)
    >{
    >	int c=0;
    >	int recordNum, quantity;
    >	char name;
    It looks like you wanted:
    char name[NAME_LEN+1];
    AWESOME MAN!!

    Always stupid stuff..

    Thanks!

    CJ
    "If it is too loud, your too old!" -unknown
    "You mean Honda Civics are not the almightiest of cars?" -My friend


    One of the funniest things I have ever heard:
    Guy at car show: "Damn that thing must do wicked burn outs."
    Me: "That is a STI! It is All Wheel Drive!"
    Guy: "Yeah...so?"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie problem: I/O File
    By Ardetcho in forum C Programming
    Replies: 4
    Last Post: 07-19-2006, 04:27 PM
  2. File I/O problem for dynamically allocated struct array
    By veecee in forum C++ Programming
    Replies: 2
    Last Post: 05-05-2006, 09:28 PM
  3. Binary File I/O Problem
    By 0rion in forum C Programming
    Replies: 10
    Last Post: 08-31-2004, 12:53 PM
  4. i/o problem...
    By Finchie_88 in forum C++ Programming
    Replies: 4
    Last Post: 08-31-2004, 11:50 AM
  5. Interesting problem with file I/O...
    By skillet in forum C++ Programming
    Replies: 12
    Last Post: 02-20-2002, 11:14 AM