View Full Version : Anybody program in C?
Xanadu
06-13-2006, 05:01 PM
so for my summative, that's due in 4 days, and I am yet to start. I chose to do a database, with movies, it includes the movie title, release year, genre, and rating. It's saved in a txt file. I need to read it in the the program and do some stuff with it. I'm thinking aboust having an add data entry, delete data entry, sort by title, sort by year, sort by genre, and sort by rating. Then it will output the new data into a new txt file. I really need to do well on this summative because I think i'm failing the course, so If anybody knows C, that would be willing to help me with stupid problems that will arise in the code, that would be greatly appreciated.
thanks guys
I have programmed in C before... not since high school, but I've done Java recently... which has some similar coding. Anyway, let me know if you need help... i still have my high school notes (sad, i know...)
Xanadu
06-15-2006, 10:07 PM
I have programmed in C before... not since high school, but I've done Java recently... which has some similar coding. Anyway, let me know if you need help... i still have my high school notes (sad, i know...)
well, for starters...this code doesnt do anything, and i dont know why, It's supposed to read in records and store them into an array of structures, then print them. But it doesnt do anything at all.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define MAX_TITLE_CHARS 50
#define MAX_MOVIES 50
#define MAX_GENRE_CHARS 20
#define MAX_RATING_CHARS 5
#define MAX_MOVIES 50
#define MAX_RECORD_CHARS 120
#define DELIMITER ','
#define NEWLINE_CHAR '\n'
#define ORIGINAL_DATABASE "/moviedatabase.txt"
typedef struct movieType
{
char title[MAX_TITLE_CHARS+1];
int year;
char genre[MAX_GENRE_CHARS+1];
char rating[MAX_RATING_CHARS+1];
} movieType, *pMovieType;
void parseMovieRecord (movieType *movie, char *record)
{
char temp [MAX_TITLE_CHARS + 1];
int i, j;
i = 0;
j = 0;
while (record [i] != DELIMITER)
temp [j++] = record [i++];
temp [j] = 0;
i++;
strcpy ( movie->title, temp);
j = 0;
while (record [i] != DELIMITER)
temp [j++] = record [i++];
temp [j] = 0;
i++;
movie->year = atoi (temp);
j = 0;
while (record [i] != DELIMITER)
temp [j++] = record [i++];
temp [j] = 0;
i++;
strcpy ( movie->genre, temp);
j = 0;
while (record [i] != NEWLINE_CHAR)
temp [j++] = record [i++];
temp [j] = 0;
strcpy (movie->rating, temp);
}
int getMovieData (movieType *movie, int *numMovies)
{
FILE *inFile;
int errorCode, i;
char record [MAX_RECORD_CHARS + 1];
if (!(inFile = fopen (ORIGINAL_DATABASE, "r")))
errorCode = -1;
else
{
i = 0;
fgets (record, MAX_RECORD_CHARS + 1,inFile);
while (!feof (inFile))
{
fgets (record, MAX_RECORD_CHARS + 1,inFile);
if (!feof (inFile))
{
if (i == MAX_MOVIES - 1)
{
errorCode = -2;
}
else
{
parseMovieRecord(&movie[i], record);
i++;
}
}
}
fclose (inFile);
*numMovies = i;
errorCode = 0;
return errorCode;
}
}
void outputMovieData (movieType *movie, int numMovies)
{
int i;
for (i = 0; i < numMovies; i++)
{
printf ("%s,", movie[i].title);
printf ("%i,", movie[i].year);
printf ("%s,", movie[i].genre);
printf ("%s,", movie[i].rating);
}
}
int main()
{
movieType movies [MAX_MOVIES];
int numMovies = 0;
getMovieData(movies, &numMovies);
outputMovieData(movies, numMovies);
system("PAUSE");
}
Xanadu
06-15-2006, 10:57 PM
nevermind...i fixed it... moviedatabase.txt was actually called moviedatabase.txt.txt
stupid XP:mad:
lol! I think I've done that before...
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.