input file

This is a discussion on input file within the C Programming forums, part of the General Programming Boards category; I'm trying to get data from an input file then manipulate it but all i want is to get the ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    13

    Angry input file

    I'm trying to get data from an input file then manipulate it but all i want is to get the data in and display it so i know that I have got it in to the program. I'm reading the data from the attached filr and here's my code so far...

    {
    char FileName[100], Data[100];
    FILE *Input, *Output;

    puts("Please enter a file name: ");
    gets(FileName);

    Input=fopen(FileName, "r");
    if(Input==NULL)
    {
    printf("Could not open the file\n");
    exit(0);
    }

    for(i=0;i<NO_OF_GYMNASTS;i++)
    {
    fgets(Competitors[i].Name, 50, Input);
    fgets(Competitors[i].Country, 50, Input);
    fscanf(Input, "%d", &Competitors[i].Age);
    for(count=0;count<NO_OF_JUDGES;count++);
    {
    fscanf(Input, "%f\n", &Competitors[i].JudgesScores[NO_OF_JUDGES]);
    }
    }

    fclose(Input);

    for(i=0;i<NO_OF_GYMNASTS;i++)
    {
    puts(Competitors[i].Name);
    puts(Competitors[i].Country);
    fprintf(Input, "%2d", Competitors[i].Age);
    for(count=0;count<NO_OF_JUDGES;count++);
    {
    fprintf(Input, "%f", &Competitors[i].JudgesScores[NO_OF_JUDGES]);
    }
    }

    puts("Please enter an output filename: ");
    gets(FileName);

    Output=fopen(FileName, "w");
    if(Output==NULL)
    {
    printf("Could not open the output file\n");
    exit(0);
    }

    for(i=0;i<NO_OF_GYMNASTS;i++)
    {
    fputs(Competitors[i].Name, Output);
    }

    fclose(Output);
    }

    ***THANK YOU FOR ANY HELP!!!***
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    65
    remove the ; from the for loops first. Then try again

    for(count=0;count<NO_OF_JUDGES;count++); <-----
    {
    fscanf(Input, "%f\n", &Competitors[i].JudgesScores[NO_OF_JUDGES]);
    }


    for(count=0;count<NO_OF_JUDGES;count++); <------
    {
    fprintf(Input, "%f", &Competitors[i].JudgesScores[NO_OF_JUDGES]);
    }

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    13

    Unhappy Input file

    I have removed the ";'s" from the end of the loops but now I am unable to get ANY of the ages or scores into the file. AAARGH!!!
    Someone please help!!!

    {
    Gymnast Competitors[NO_OF_GYMNASTS];

    int i;
    int count;


    char FileName[100], Data[100];
    FILE *Input, *Output;

    puts("Please enter a file name: ");
    gets(FileName);

    Input=fopen(FileName, "r");
    if(Input==NULL)
    {
    printf("Could not open the file\n");
    exit(0);
    }

    for(i=0;i<NO_OF_GYMNASTS;i++)
    {
    fgets(Competitors[i].Name, 50, Input);
    fgets(Competitors[i].Country, 50, Input);
    fscanf(Input, "%d\n", &(Competitors[i].Age));
    for(count=0;count<NO_OF_JUDGES;count++)
    {
    fscanf(Input, "%f\n", &Competitors[i].JudgesScores[NO_OF_JUDGES]);
    }
    }

    fclose(Input);

    for(i=0;i<NO_OF_GYMNASTS;i++)
    {
    puts(Competitors[i].Name);
    puts(Competitors[i].Country);
    fprintf(Input, "%d\n", Competitors[i].Age);
    for(count=0;count<NO_OF_JUDGES;count++)
    {
    fprintf(Input, "%f\n", &Competitors[i].JudgesScores[NO_OF_JUDGES]);
    }
    }

    puts("Please enter an output filename: ");
    gets(FileName);

    Output=fopen(FileName, "w");
    if(Output==NULL)
    {
    printf("Could not open the output file\n");
    exit(0);
    }

    for(i=0;i<NO_OF_GYMNASTS;i++)
    {
    fputs(Competitors[i].Name, Output);
    }

    fclose(Output);
    }
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    65
    for(count=0;count<NO_OF_JUDGES;count++)
    {
    fprintf(Input, "%f\n", &Competitors[i].JudgesScores[NO_OF_JUDGES]);
    }

    Remove the & before Competitors[i].JudgesScore .....

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    104

    Lightbulb

    You know what's bothering me is that you are apparently using structures but haven't declared any.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 01:44 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-22-2008, 12:19 AM
  3. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 02:04 PM
  4. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 04:49 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM

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