Problem with file i/o

This is a discussion on Problem with file i/o within the C Programming forums, part of the General Programming Boards category; Hi, I am trying to make a program that reads a txt file and sets the information into a linked ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    14

    Problem with file i/o

    Hi,

    I am trying to make a program that reads a txt file and sets the information into a linked list. It is hard for me to explain it, so i will give an example:

    The txt file:


    Master Rune of Snorri Spangelhelm-------------------------------75 pts

    Any blows struck by a weapon engraved with this
    rune will always hit. No roll ot his necessary.

    Master Rune of Skalf Blackhammer--------------------------------75 pts

    Any weapon Bearing this rune will automatically
    wound if it hits. No roll to wound required.
    Use characters strength for hte save modifier.

    Mater Rune of Alaric the Mad--------------------------------------50 pts

    This rune cancels an opponents armour saving throw.
    When wounded by this weapon, the target is not
    allowed an armour saving throw of any kind.

    End of txt file

    I cant get it to read the name of the rune, point cost of the rune, and the discription. I am trying to either get it into either an array or a linked list. can you help... any ideas would helo

  2. #2
    Registered User
    Join Date
    Dec 2003
    Posts
    53
    post the part of the code where you are having trouble.
    I think using fgets and sscanf should be able to get most of the data into the appropriate places. using strtok and having a special character to denote separate parts of teh text may help.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    14
    ok... well the thing is i dont know what i am doing wrong. i am able to read the file ok but, when i get to the second fgets the program messes up. what am i doing wrong?
    everything with the // are posibilities but what is the right way.

    #include <stdio.h>

    char subsection [30];
    char discription [500];
    char discriptiontemp [80];
    int points=0;
    int num_of_lines=0;
    int i=0;


    int main()
    {
    FILE *inputfile;


    inputfile = fopen("Rune2.txt","r");

    if (inputfile == NULL)
    {
    printf("error opening file");
    }
    else
    {

    printf("File opened successfully. Loading data...\n");
    //fscanf(inputfile, "%s", subsection);

    fgets(subsection, 100, inputfile);
    fscanf(inputfile, "%d", &points);
    fscanf(inputfile, "%d", &num_of_lines);
    //fgets(&points, 10, inputfile);
    //fgets(&num_of_lines, 10, inputfile);
    //inputfile++;
    while(i!=num_of_lines)
    {

    //fscanf(inputfile, " %s ", discription);
    //fgets(points, 10, inputfile);
    //fgets(num_of_lines, 10, inputfile);
    fgets(discriptiontemp, 100, inputfile);
    strcat(discription, discriptiontemp);
    i++;
    //inputfile++;
    }

    printf("%s \n%d pts \n%s", subsection, points, discription);



    }

    }

  4. #4
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Why are you mixing fscanf with fgets just use fgets and sscanf

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    And while you're at it...

    Use [code] tags!

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Change your file so it's easier to deal with:
    Master Rune of Snorri Spangelhelm
    75 pts
    Any blows struck by a weapon engraved with this
    rune will always hit. No roll ot his necessary.

    Master Rune of Skalf Blackhammer
    75 pts
    Any weapon Bearing this rune will automatically
    wound if it hits. No roll to wound required.
    Use characters strength for hte save modifier.

    Mater Rune of Alaric the Mad
    50 pts
    This rune cancels an opponents armour saving throw.
    When wounded by this weapon, the target is not
    allowed an armour saving throw of any kind.
    Read first line, rune name (fgets)
    Read second line, points (fgets, sscanf)
    Loop to read next lines until an empty line (fgets, strcat)
    Repeat until eof
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    ohhhh purty colors quzah

  8. #8
    Registered User
    Join Date
    Apr 2004
    Posts
    14
    So... if i am getting this correct. i need to store the input of the fgets() into an array and then go through the array and break it up into seperate components?

  9. #9
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Quote Originally Posted by Dragoncaster131
    So... if i am getting this correct. i need to store the input of the fgets() into an array and then go through the array and break it up into seperate components?
    Almost.
    If you are talking about my idea then you'd create 3 arrays:
    1) array of characters for the number of runes
    2) array of ints for the points
    3) array of characters for the description.

    Assuming the longest rune name is 25 and you have 10 runes
    char RuneName[10][26];
    would created an array for the runes. 26 includes the trailing 0 to terminate the string.

    printf("%s", RuneName[3]); will print the name of the 4th rune
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File i/o problem
    By tezcatlipooca in forum C++ Programming
    Replies: 18
    Last Post: 01-01-2007, 08:01 AM
  2. File I/O problem
    By Onions in forum C++ Programming
    Replies: 41
    Last Post: 02-24-2006, 03:32 PM
  3. File I/O problem
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 12
    Last Post: 09-03-2005, 12:14 PM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM

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