Thread: Database using C

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    5

    Database using C

    Hi! I have an assignment and need to do database using C. I tried starting it and this is what I came up with so far. I'm still a beginner but I tried indenting it as well as I could (sorry if it's not good enough!). Now my problem is that an error came up and it won't give me the line where the error is (I'm suspecting 50errors or so will come up after this one is solved). Can someone please find me some things which I'm doing wrong and explain to me the exact use of fseek, because even though I'm using it, I still can't fully get what it's used for. Below is my source code, many thanks!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "ddd.h"
    
    int menu()
    {
    	int choice = 0;
    
    	do
    	{
    		printf("Welcome to the F1 database, please choose one of the following options:\n");
    		printf("\t Press 1 to update the driver's details.\n");
    		printf("\t Press 2 to enter the race results.\n");
    		printf("\t Press 3 to see the driver's current standings.\n");
    		printf("\t Press 4 to exit the program.\n");
    		scanf("%d", &choice);
    	}
    	while ((choice < 1) && (choice > 4));
    	{
    		printf("\a This is an invalid option, please try again.\n");
    	}
    
    
    	return choice;
    }
    
    
    int main()
    {
    	int choice = 0;
    	int recordNum = 0;
    	Driver driver = {0};
    
    	do 
    	{
    		choice = menu();
    		switch (choice)
    		{
    		case 1://Updating the driver's details
    			updatecurrentrecord(&driver);
    			addRecord(FILENAME, &driver);
    			break;
    
    		case 2://Entering the race results
    			//enterresults;
    			break;
    
    		case 3://Displaying the driver standings
    			printDriverstandings(listHead);
    			break;
    
    		default:
    			break;
    		}
    	}
    	while (choice != 4);
    
    }
    
    
    //Case 1
    int updatecurrentrecord(const char *filename, int recordNum, Driver *driver)
    {
    	int ret = 0;
    	int carnumber = 0;
    	FILE *file = fopen(filename, "r+b");
    
    	system("cls");
    	printf("Please enter the car number which you would like to add or update : ");
    	scanf("%d",driver->carnumber);
    
    	while ((carnumber < 1) || (carnumber > 26))
    	{
    		printf("Enter a valid car number between 1 and 26, please try again. \a \n");
    		scanf("%d",driver->carnumber);
    	}
    	
    	fclose(file);
    	return ret;
    }
    
    void enterdriverdetails(Driver *driver);
    {
    	if (fseek(file, sizeof(Driver) * carnumber, SEEK_SET) != 0)
    	{
    		FILE *file = fopen(filename, "w+b");
    		system("cls");
    		printf("The car number chosen has no details ");
    		printf("Please enter the driver's full name: ");
    		scanf("%s", driver->drivername);
    		printf("Please enter the car's constructor team name: ");
    		scanf("%s", driver->teamname);
    		printf("Please enter the number of points earned so far in the Formula 1 season: ");
    		scanf("%d", &driver->points);
    	}
    
    }
    
    int updatecurrentrecord(const char *filename, int recordNum, Driver *driver)
    {
    	fopen(file);
    
    	if (fseek(file, sizeof(Driver) * recordNum, SEEK_SET) == 0)
    	{
    		system("cls");
    		printf("The car number chosen akready has a driver! Please choose one of the following options: \n");
    		printf("\t Press 0 to change the driver's name. \n");
    		printf("\t Press 1 to change the car's constructor team name. \n");
    		scanf("%d", &correctionchoice);
    	}
    
    	while ((correctionchoice < 0) || (correctionchoice > 1))
    	{
    		printf("Please enter a valid choice, please try again. \a \n");
    	}
    
    	if (correctionchoice == 0)
    	{
    		printf("Please enter the driver's new full name: ");
    		scanf("%s", driver->drivername);
    	}
    
    	else
    	{
    		printf("Please enter the car's new constructor team name: ");
    		scanf("%s", driver->teamname);
    	}
    
    	fclose(file);
    	return ret;
    }
    .
    Last edited by dmag0006; 04-19-2011 at 04:37 AM.

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by dmag0006 View Post
    Now my problem is that an error came up and it won't give me the line where the error is (I'm suspecting 50errors or so will come up after this one is solved).
    You're asking people to look into a crystal ball. If no line number is generated then you probably have a linker error - please post the error (I must stress you should post the very first error you get, even if you have to scroll up to see it...) and we might have some chance of helping you.


    Quote Originally Posted by dmag0006 View Post
    ...and explain to me the exact use of fseek, because even though I'm using it, I still can't fully get what it's used for.
    I don't know if you're using Linux or not, but the man pages for the C standard library should still be useful - here's the one for fseek().

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    5
    Quote Originally Posted by JohnGraham View Post
    You're asking people to look into a crystal ball. If no line number is generated then you probably have a linker error - please post the error (I must stress you should post the very first error you get, even if you have to scroll up to see it...) and we might have some chance of helping you.
    Thanks! This is the error below,

    1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup





    Quote Originally Posted by JohnGraham View Post
    I don't know if you're using Linux or not, but the man pages for the C standard library should still be useful - here's the one for fseek().
    Uhh I don't know what Linux is to be honest and it was never mentioned to me during the lessons I was given, so I don't think I use it. Thanks for the link though!

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    First of all main should return a value, 0 at the end.

    Secondly, if you want to write the function implementations after main, you have to provide their prototypes ahead of main so that the compiler knows what you are talking about.

    So add the first line of every function before main with a semicolon at the end.

    e.g. int updatecurrentrecord(const char *filename, int recordNum, Driver *driver);

    Your reply about Linux is pure gold, although it is ok not to be familiar with what it is.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    5
    I fixed the main.

    I think you're referring to the header file, if so it's already there (just in the header file).

    As for the Linux, I tried being as clear as possible ;]

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by dmag0006 View Post
    Now my problem is that an error came up and it won't give me the line where the error is (I'm suspecting 50errors or so will come up after this one is solved).Can someone please find me some things which I'm doing wrong
    Here's the #1 thing you did wrong: you wrote a lot of code you could not test because the program has never run without errors. There are two reasons someone might do that:

    1) Because they did not have access to a compiler when they were doing their homework.
    2) Because they think that is a good way to code: first you write the entire program, then you try to run it and start debugging!
    3) Because they thing it is impossible to do anything else: how can I test the program before it is finished?

    If the reason is #1, don't bother doing this again. If you have a computer, you can get a C compiler for it. Writing code without a compiler is absurdly foolish if you are still learning the fundamentals of the language.

    If the reason is #2, you are very wrong, as you are hopefully coming to realize.

    If the reason is #3, you are even more wrong. You should be able to test compile, on this scale, every 5-10 lines of code, or every 5-10 minutes, which ever comes first, at most. And you do not proceed further until it compiles without errors. It does not matter if the resulting executable does not do anything, but you should try to aim to have something to evaluate there too even if it means shimming in a few temporary lines for output.

    If you did a survey of software developers to find out how many write an entire application without doing any compiling or debugging, because they leave that until later, the answer will be exactly 0.0%. No one writes this way. It is a huge mistake sometimes made by people who are brand new to the art. Never, never, never do it again.

    It's impossible for me to say what your error is about because without the header, I can't try to compile this either.

    The purpose of fseek is to re-position a file pointer. When you open a file, the file pointer is at the beginning. As you read thru it, it advances into the file. You can skip forward or back via fseek:

    File Positioning - The GNU C Library
    fseek

    Those are GNU and POSIX oriented references but this particular information pertains to standard C stuff.
    Last edited by MK27; 04-19-2011 at 05:46 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    5
    This is my header file

    Code:
    #define FILENAME "drivers.dat"
    
    typedef struct Driver
    {
    	char drivername[25];
    	char teamname[25];
    	int carnumber;
    	int points;
    } Driver;
    
    void enterdriverdetails(Driver *driver);
    int updatecurrentrecord(const char *filename, int recordNum, Driver *driver);
    int main();
    int menu();
    int getUserRecordNum();
    void displayDriver(const Driver* pDriver);
    void printDriverstandings(Driver* listHead);
    I continued because this error turned up from the first time I tried compiling it, and I couldn't afford getting stuck because of an error where not even the line is given to me =/

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Sadly, I'm not getting that linker error, perhaps because I am using a different compiler/linker, but very likely it has to do with how you are using whatever compiler you are using.

    So, what compiler are you using? Are you using it as part of an IDE? If you let us know, someone here who uses the same software might be able to help.

    After that, here's your first wave of errors:

    Code:
    test.c: In function ‘main’:
    test.c:41:4: warning: passing argument 1 of ‘updatecurrentrecord’ from incompatible pointer type
    ddd.h:12:5: note: expected ‘const char *’ but argument is of type ‘struct Driver *’
    test.c:41:4: error: too few arguments to function ‘updatecurrentrecord’
    ddd.h:12:5: note: declared here
    test.c:50:25: error: ‘listHead’ undeclared (first use in this function)
    test.c:50:25: note: each undeclared identifier is reported only once for each function it appears in
    test.c: At top level:
    test.c:84:1: error: expected identifier or ‘(’ before ‘{’ token
    test.c:100:5: error: redefinition of ‘updatecurrentrecord’
    test.c:63:5: note: previous definition of ‘updatecurrentrecord’ was here
    test.c: In function ‘updatecurrentrecord’:
    test.c:102:8: error: ‘file’ undeclared (first use in this function)
    test.c:102:2: error: too few arguments to function ‘fopen’
    /usr/include/stdio.h:271:14: note: declared here
    test.c:110:16: error: ‘correctionchoice’ undeclared (first use in this function)
    test.c:131:9: error: ‘ret’ undeclared (first use in this function)
    Not so bad, actually your code is fairly well written IMO based on glancing at it.

    Quote Originally Posted by dmag0006 View Post
    I continued because this error turned up from the first time I tried compiling it, and I couldn't afford getting stuck because of an error where not even the line is given to me =/
    That does suck, in the future resist temptation. It is not going to take you any less time to solve it now, so the idea that you might have been saving yourself some by ignoring it earlier is fallacious! OTOH, at least you can relax knowing most of the work is probably done...

    To re-iterate: I really don't think that linker error has anything to do with your code, I think it is some issue with how you are using your compiler. If you can get that straightened out, it should go away. But I don't work on MS systems, so I'm not much help with that. Good luck!
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Interresting that nobody mentions he has two functions named updatecurrentrecord()

  10. #10
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    My compiler says...
    updatecurrentrecord function does not take 1 argument.
    There are many more errors.

  11. #11
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Yes I agree with nonoob there are a ton of compiler errors and probably even more logical ones. If you are having a an environment problem, download Pelles or Codeblocks with MinGw and use that as your IDE from now on. It should work fine on the first try.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Database to Database Transfer?
    By draggy in forum C++ Programming
    Replies: 4
    Last Post: 01-17-2007, 10:50 AM
  2. Database v.2.2
    By nomi in forum C Programming
    Replies: 7
    Last Post: 01-20-2004, 06:42 AM
  3. Database On the NET
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 02-19-2003, 08:44 AM
  4. Replies: 1
    Last Post: 10-09-2001, 10:20 PM
  5. c database
    By ggs in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 08-10-2001, 01:30 PM