Thread: Storing information created files.

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

    Storing information created files.

    I created a file with this code. Now I need to read the file back and save all the information.... I am finding very little help online concerning file processing; I saw the other post about saving an array from a file and I took salem's adviced and looked for examples but didn't have much luck. Can anyone point me in the right direction?

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <conio.h>
    
    int main()
    
    {
    
    	int i;
    
    	FILE *outFile;
    
    	int arrayMaxSize = 6;
    
    		char *carOwner[] = {
    						      "John Jones",
    						      "Mary James",
    						      "Amy Smith",
    						      "Joe Johns",
    							  "Long Name",
    							  "Mary Ames"};
    
    		int carNo[] = {    
    						      55,
    						      67,
    						      99,
    						      88,
    							  56,
    							  97};
    
    		
    		int milesDriven[] = { 
    						      250,
    						      350,
    						      100,
    						      123,
    							  1000,
    							  238};
    
    		int gallonsUsed[] = { 
    						      20,
    						      10,
    						      8,
    						      86,
    							  80,
    							  15};
    
    
    	outFile = fopen("test.dat", "w");
    
    	if (outFile == NULL)
    	{
    		printf("\nFailed to open the file.\n");
    		exit(1);
    	}
    
    	//fprintf(outFile, "Car Owner      CarNo     Miles Driven     Gallons Used\n\n");
    
    	for (i = 0; i < arrayMaxSize; i++)
    		fprintf(outFile, "%-10s %7d %15d %15d\n", carOwner[i], carNo[i], milesDriven[i], gallonsUsed[i]);
    	fclose(outFile);
    
    _getch();
    
    return 0;
    
    }//END OF MAIN

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    I don't even know what you're trying to do.

    Are you just trying to write a new program to read through the same file and load all of the struct information?

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    I need to pull those numbers back in from reading the file and then work with them so I can add more things.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Why? If you just wrote them to the file, they're still in memory in your program. You can still work with them.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    I dunno here is the problem

    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.



    Turn in the pseudocode, program, sample output, and .exe file. Place all of the items in an envelope. Turn in the project on the day of the Final Exam. No project will be accepted, otherwise.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    *sigh*

    You're supposed to manually create a file with that data, and then write a program to read the data from the file, and then spit out the output as specified.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    Well either way I have the file, so how am I suppose to pull out just the numbers and then add them up?

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Come on, man, this is your assignment. Think about it for a second.

    You know exactly how the input will be. You can read it exactly as needed. Here's one way: Write a function to read off an entire struct's worth of this stuff line by line. Then call that function in a loop to keep reading.

    If you're asking how to read from a file, then get thee to a search engine and search.

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    I have this so far. I understand pulling a line out... I think. But how do I get just piece of a line...
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    int main()
    {
    char line[81]={0}, owner_Name[15]={0}
    
    int carNo = 0;
    int miles_Driven = 0;
    int gallons_Used = 0;
    
    int gas_Total = 0
    int miles_Total = 0
    
    FILE *inFile;
    
    printf("\nEnter an input file name: ");
    gets(fileName);
    
    inFile = fopen(test.dat,"r");
    if (inFile == NULL)
    {
    printf("\nFailed to open the %s file.\n", fileName);
    exit(1);
    }
    
    while (fgets(line,81,inFile) != NULL)
    {
    sscanf(line, "%s %d %d %d", owner_Name, carNo, miles_Driven, gallons_Used);
    that look at all close for a start?

  10. #10
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    Ok I got it to finally read all in! LOL WOO! Now I just need to figure out how to pull it apart... Where should I start.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    int main()
    {
    
    
    char line[81];
    
    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)
    printf("%s", line);
    
    
    
    
    
    
    return 0;
    }

  11. #11
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    For some reason my program is only reading in the last line.... Do you know why? I figured out how to pull the data!

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    int main()
    {
    
    
    char line[81];
    
    char firstName[10] = {0};
    char lastName[10] = {0};
    
    int carNo = 0;
    int miles_Driven = 0;
    int gallons_Used = 0;
    
    
    int miles_Total = 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 %s %d %d %d", firstName, lastName, &carNo, &miles_Driven, &gallons_Used);
    
    //miles total =
    
    }
    
    printf("%s %s %d %d %d", firstName, lastName, carNo, miles_Driven, gallons_Used);

  12. #12
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    LOL. You wrote the code, come on. You're reading in each line in a loop, and then only printing the last one because the printf() is outside of the loop.

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,170
    Your printf is OUTSIDE the while loop, that's why it only happens once.

    Oh, and try to improve your indentation.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Working with muliple source files
    By Swarvy in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2008, 08:36 AM
  2. Storing Information then Displaying
    By peckitt99 in forum C++ Programming
    Replies: 11
    Last Post: 09-05-2006, 11:29 AM
  3. files size to char[]
    By purefan in forum C++ Programming
    Replies: 1
    Last Post: 04-03-2005, 08:43 PM
  4. displaying text files, wierd thing :(
    By Gades in forum C Programming
    Replies: 2
    Last Post: 11-20-2001, 05:18 PM
  5. Storing information in a file
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 11-18-2001, 09:05 AM