Thread: Random access files and structures

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    I am Diamond Dave
    Join Date
    Mar 2006
    Location
    You'll get some leg tonight for SURE!
    Posts
    34

    Random access files and structures

    Right now I'm trying to read in a buncho f data from the text file "Top_Movies.txt" and putting the data into a structure. This is what I have so far. Now I know it isn't goign to work so I'm not even going to bother trying to compile it...but am I even close? I think I am but what do I know? I've also included the text file, which I do not know how to include in the actual program:

    Code:
    #include <stdio.h>
    
    
    struct Movies {
    	int rank;
    	float rating;
    	char title[25];
    	char director[25];
    };
    
    int main()
    {
    	struct Movies list[50];
    	int ranking;
    	float rate;
    	char movie[25];
    	char bywhom[25];
    	int i;
    	
    	
    	FILE *myFile;
    
    	myFile = fopen("Top_Movies.txt", "r");
    		if (myFile == NULL) {
    			printf("Unable to open Top_Movies.txt.\n");
    		
    		else
    		{ for (i=0;i<50;i++)
    			{
    				fscanf( myFile, "%d%.1f%c%c", &ranking, &rate, movie, bywhom);
    				
    				list[i].rank = ranking;
    				list[i].rating = rate;
    				list[i].title = movie;
    				list[i].director = bywhom;
    		}
    		} 
    		
    		fclose(myFile);
    		return 0;
    
    
    		}
    here's the txt file:

    1, 9.1, The_Godfather, 1972, Francis_Ford_Coppola
    2, 9, The_Shawshank_Redemption, 1994, Frank_Darabont
    3, 8.9, The_Godfather:_Part_II, 1974, Francis_Ford_Coppola
    4, 8.8, The_Lord_of_the_Rings:_The_Return_of_the_King_, 2003, Peter_Jackson
    5, 8.8, Shichinin_no_samurai, 1954, Akira_Kurosawa
    6, 8.8, Schindler's_List, 1993, Steven_Spielberg
    7, 8.7, Casablanca, 1942, Michael_Curtiz
    8, 8.7, Star_Wars:_Episode_V_-_The_Empire_Strikes_Back, 1980, Irvin_Kershner
    9, 8.7, Pulp_Fiction, 1994, Quentin_Tarantino
    10, 8.7, Star_Wars, 1977, George_Lucas
    11, 8.7, Il_Buono_il_brutto_il_cattivo, 1966, Sergio_Leone
    12, 8.7, One_Flew_Over_the_Cuckoo's_Nest, 1975, Milos_Forman
    13, 8.7, The_Lord_of_the_Rings:_The_Fellowship_of_the_Ring, 2001, Peter_Jackson
    14, 8.7, Rear_Window, 1954, Alfred_Hitchcock
    15, 8.6, Cidade_de_Deus, 2002, Fernando_Meirelles
    16, 8.6, The_Usual_Suspects, 1995, Bryan_Singer
    17, 8.6, The_Lord_of_the_Rings:_The_Two_Towers, 2002, Peter_Jackson
    18, 8.6, Raiders_of_the_Lost_Ark, 1981, Steven_Spielberg
    19, 8.6, 12_Angry_Men, 1957, Sidney_Lumet
    20, 8.6, Dr._Strangelove_or:_How_I_Learned_to_Stop_Worrying _and_Love_the_Bomb, 1964, Stanley_Kubrick
    21, 8.6, Citizen_Kane, 1941, Orson_Welles
    22, 8.6, Memento, 2000, Christopher_Nolan
    23, 8.6, Psycho, 1960, Alfred_Hitchcock
    24, 8.6, Goodfellas, 1990, Martin_Scorsese
    25, 8.6, C'era_una_volta_il_West, 1968, Sergio_Leone
    26, 8.5, North_by_Northwest, 1959, Alfred_Hitchcock
    27, 8.5, The_Silence_of_the_Lambs, 1991, Jonathan_Demme
    28, 8.5, Lawrence_of_Arabia, 1962, David_Lean
    29, 8.5, Le_Fabuleux_destin_d'Amelie_Poulain, 2001, Jean-Pierre_Jeunet
    30, 8.5, It's_a_Wonderful_Life, 1946, Frank_Capra
    31, 8.5, Sunset_Blvd., 1950, Billy_Wilder
    32, 8.5, Fight_Club, 1999, David_Fincher
    33, 8.5, American_Beauty, 1999, Sam_Mendes
    34, 8.4, The_Matrix, 1999, Andy_Wachowski/Larry_Wachowski
    35, 8.4, Vertigo, 1958, Alfred_Hitchcock
    36, 8.4, Eternal_Sunshine_of_the_Spotless_Mind, 2004, Michel_Gondry
    37, 8.4, Taxi_Driver, 1976, Martin_Scorsese
    38, 8.4, Apocalypse_Now, 1979, Francis_Ford_Coppola
    39, 8.4, Paths_of_Glory, 1957, Stanley_Kubrick
    40, 8.4, To_Kill_a_Mockingbird, 1962, Robert_Mulligan
    41, 8.4, Der_Untergang, 2004, Oliver_Hirschbiegel
    42, 8.4, Se7en, 1995, David_Fincher
    43, 8.4, The_Pianist, 2002, Roman_Polanski
    44, 8.4, Chinatown, 1974, Roman_Polanski
    45, 8.4, Leon, 1994, Luc_Besson
    46, 8.4, Sen_to_Chihiro_no_kamikakushi, 2001, Hayao_Miyazaki
    47, 8.4, Hotel_Rwanda, 2004, Terry_George
    48, 8.4, American_History_X, 1998, Tony_Kaye
    49, 8.3, The_Third_Man, 1949, Carol_Reed
    50, 8.3, Monty_Python_and_the_Holy_Grail, 1975, Terry_Gilliam/Terry_Jones
    Last edited by DLR; 04-19-2006 at 09:33 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Create Copies of Files
    By Kanshu in forum C++ Programming
    Replies: 13
    Last Post: 05-09-2009, 07:53 AM
  2. How to create a C project with multiple source files
    By MSF1981 in forum C Programming
    Replies: 5
    Last Post: 03-22-2009, 09:25 AM
  3. header and source files
    By gtriarhos in forum C Programming
    Replies: 3
    Last Post: 10-02-2005, 03:16 AM
  4. structures and files
    By eth0 in forum C++ Programming
    Replies: 9
    Last Post: 01-06-2004, 06:48 PM
  5. Random File Issues
    By johnnyd in forum C Programming
    Replies: 5
    Last Post: 03-28-2002, 01:15 PM