Thread: input from text using structures

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    10

    input from text using structures

    hello i'm confused on how to start creating this program. I am clear on how to open and read a text file. The objective is to read the text file that starts with first line with the first variable being how many lines of statements are in the rest of the file. second variable for how many intergers in each statement. i know "malloc" will input each inerger by how many i need. BUT how can i make the structure read how many intergers are on each line without knowing before hand the # of intergers i need. Here is an example of the text input.

    3 4
    Boston Starlite 300 410 400 600 JimRussel
    Michigan 3Dlabs 200 120 150 300 StephenWilliams
    Michigan Albros 400 420 510 550 BillCarter

    and i know data structures reads like this

    Code:
    struct salesData sales = {"", "", 0, 0, 0, 0, ""};
    when displaying data in the text file. how do i code it in the structure for the number of intergers that are randomly inputted because the numbers of intergers will increase or decrease with each different text file.


  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    For the integers, you could put a pointer to an int in the structure and from the number obtained from the file then dynamically allocate an array of that size.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    10
    then how would i also get it to read the strings that are the same line?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Well, I'd use char arrays for those structure elements if their size was within a known limit.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    10
    is this wat u mean?

    Code:
     struct salesData{
          char city[10];
          char company[15];
          char name[20];
       };
    so wat would i do to read the intergers within the line in the text file

    Boston Starlite 300 410 400 600 JimRussel

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Right, like I said before:
    Code:
    struct salesData
    {
       char city[10];
       char company[15];
       char name[20];
       int *value; /* size TBD at runtime */
    };
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    10
    then i would make the structure read the strings lke this

    Code:
    struct salesData sales = {"", "",  ""};
    so i would just skip the interger at first and make a function read the pointer
    Last edited by ehj3000; 09-16-2005 at 08:54 PM.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Time to tell us which way it is: you have a static structure with a compile-time definition and initialization, or you have a dynamic structure that is populated at run-time. If it's at run-time which I have assumed thus far, then the compile-time example is not terribly relevant.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Sep 2005
    Posts
    10

    Question

    i believe dynamic structue at run-time. it's just got confusing when the i was typing the code in and can't get it read to the intergers when i run the program.

    Code:
    struct salesData
    {
       char city[10];
       char company[15];
       char name[20];
       int *value; /* size TBD at runtime */
    };
    Code:
    struct salesData sales = {"", "", x, x, x, x, ""};
    What I am trying to accomplish is to have the values displayed in the location of the 'x' of struct line of code. The text file is already created and stored in the same directory as the program. Its a matter of printing it as if it weren't defined variables.

    The display is supposed to print to the screen as -
    Boston Starlite 300 410 400 600 JimRussel
    Michigan 3Dlabs 200 120 150 300 StephenWilliams
    Michigan Albros 400 420 510 550 BillCarter
    Last edited by ehj3000; 09-16-2005 at 09:48 PM.

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Break things into smaller steps. If you had two integers for the first line of the file such as count and sales, you might have this:
    Code:
          if ( fscanf(file, "%d %d%*c", &count, &sales) == 2 )
    Then you might make a dynamic array of such records...
    Code:
             struct salesData *rec = malloc(count * sizeof *rec);
    And then within each line you might read it something like this (between reading the first two strings and the last one)...
    Code:
                   rec[i].value = malloc(sales * sizeof rec[i].value);
                   if ( !rec[i].value )
                   {
                      return 0; /* better cleanup should be done */
                   }
                   for ( j = 0; j < sales; ++j )
                   {
                      if ( fscanf(file, "%d", &rec[i].value[j]) != 1 )
                      {
                         return 0; /* better cleanup should be done */
                      }
                      printf("%d\n", rec[i].value[j]);
                   }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    Registered User
    Join Date
    Sep 2005
    Posts
    1
    ^^^I don't think that would print it the way it's supposed to be styled.

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I'm sorry, I'm just trying to help you with your assignment -- not do it for you. Are you asking me to stop?
    Last edited by Dave_Sinkula; 09-16-2005 at 11:19 PM. Reason: I'm sorry for my retort here, I did not pay enough attention to who was posting. Late & cheap beer & etc. ehj - check your Private Messages.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #13
    Registered User
    Join Date
    Sep 2005
    Posts
    10
    Quote Originally Posted by skd126
    ^^^I don't think that would print it the way it's supposed to be styled.

    man he doesn't know wat he's talking about. dave i appreciate the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Evaluation of structure's element against input not working!
    By laczfinador in forum C Programming
    Replies: 6
    Last Post: 05-11-2009, 01:19 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Input output with structures
    By barim in forum C Programming
    Replies: 10
    Last Post: 04-27-2004, 08:00 PM
  4. text input buffer clearing
    By red_Marvin in forum C++ Programming
    Replies: 4
    Last Post: 03-20-2003, 03:17 PM
  5. text input in graphics mode
    By jamie in forum C++ Programming
    Replies: 1
    Last Post: 03-17-2003, 10:33 PM