File Manipulation Help

This is a discussion on File Manipulation Help within the C Programming forums, part of the General Programming Boards category; Here is my problem. I have the code I've written so far below. Can anyone help!? lol A. Create a ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    133

    File Manipulation Help

    Here is my problem. I have the code I've written so far below. Can anyone help!? lol

    A. Create a file containing the following data about cars.


    Car Owner
    Car No.
    Miles Driven
    Gallons Used

    John Jones
    55
    250
    20

    Mary James
    67
    350
    10

    Amy Smith
    99
    100
    8

    Joe Johns
    88
    123
    86

    Trudy Mains
    56
    1000
    80

    Mary Ames
    97
    238
    15

    B. Write a C program that reads the data in the file created in part A and displays the owner's name, car number, miles driven, gallons used and the miles per gallon for each car. The output should additionally contain the total miles driven, total gallons used, and average miles per gallon for all the cars. These totals should be displayed at the end of the output report.



    Divide the program into appropriate functions.


    This is what I've come up with so far. I'm not sure if I'm doing this right at all but it's better to attempt it than do nothing. So here it goes... I keep gettin these run errors after it's compiled!

    --------------------Configuration: Cpp1 - Win32 Debug--------------------
    Linking...
    pgm10-4.obj : error LNK2005: _main already defined in Cpp1.obj
    final.obj : error LNK2005: _main already defined in Cpp1.obj
    Debug/Cpp1.exe : fatal error LNK1169: one or more multiply defined symbols found
    Error executing link.exe.

    Cpp1.exe - 3 error(s), 0 warning(s)


    Code:
    //Doug Miller
    //Final Program, writing information to a file
    
    
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <conio.h>
    
    int main()
    
    {
    
    
    	char line[81] = {0};
    	char carOwner[10] = {0};
    	int carNo[4] = {0};
    	int milesDriven[10] = {0};
    	int gallonsUsed[10] = {0};
    	
    	FILE *inFile;
    
    	inFile = fopen("test.dat", "r");
    	
    	if (inFile == NULL)
    	{
    		printf("\nFailed to open the file.\n");
    		exit(1);
    	}
    
    
    
    
    while (fgets(line,81,inFile) != NULL)
    {
    sscanf(line, "%s %d %d %d", carOwner, carNo, milesDriven, gallonsUsed);
    printf("\nName: %s\nCar No: %d\nMiles Drien %d\nGallons Used:%d\n", carOwner, carNo, milesDriven, gallonsUsed);
    }
    
    
    fclose(inFile);
    printf("\nThe file has been successfully written.\n");
    
    _getch();
    
    return 0;
    
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,762
    Only one source file can contain main()
    It seems all your source files contain main()
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Apparently you have a few source files (pgm10-4.c, cpp1.c, and final.c) being linked in your program and each one has a main function in it. There should only be one main function per complete program.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 04:53 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 11:51 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21