Very basic question

This is a discussion on Very basic question within the C Programming forums, part of the General Programming Boards category; hello i am very new to the whole programming thing in general and just learning the basics of syntax etc ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    7

    Unhappy Very basic question

    hello

    i am very new to the whole programming thing in general and just learning the basics of syntax etc of the C programming language. I am currently trying to complete an assignment for school that at this point seems nearly impossible, though im sure its all easy as pie for you guys. So without further ado heres my simple issue.

    I have a file that holds a value on each line eg.

    2.56
    2.43
    6.67
    etc
    etc

    what i need to do is, create an array and read each value from the input file and store them individually in a different element in the array. Now i know this may seem overly simple to actually bother with, however i would greatly appreciate any help given. Ive gotten to the point of creating ] the array and opening the file so it can be read, however after that point the actual scanning (fscanf i guess would be used) and storing of the values into the array is all a mess to me.
    Last edited by something___; 12-06-2004 at 02:08 PM.

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Show some code

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    7
    okay well thing is didnt really even know how to tackle the problem but ill see what i can do....

    its something like this in the main function
    -----------------------------------
    double array[8];

    fp = fopen("Input.in", "r");
    /* sadly this is as far as i seem to be able to get */
    /* then from what ive gathered the rest would consist of a*/
    fscanf("%lf", &array[]);


    -------------------------------
    contents of Input.in
    ------------------------------

    5.43
    4.45
    4.42
    4.45
    4.25
    8.63
    etc etc
    -------------------------------

    what i assume i should do to tackle this problem is make it so the fscanf scans until it reaches a new line, then taking the value before the new line and placing it into an element in the array. Then continuing to scan skipping the first line to scan the second value on line 2 and placing it into element 2 in the array. And continuing etc etc until it reaches the end of the file.

    Thats my idea...i could be amazingly wrong but thats what i think would solve my problem but i have no idea how to actually punch it out

  4. #4
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Have you learned loops yet, is there a set number of elements in the file?

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    7
    yes ive learned about loops but not really understanding too well i guess....or maybe its just not coming to me. And there is 8 elements in the input file.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Uh, how about a loop then, say something like this?
    Code:
       int i;
       for ( i = 0; i < 8; ++i )
       {
          fscanf(fp, "%lf", &array[i]);
       }
    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
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Using them with arrays is a good way to understand loops. In the above example, it's just an example of when you have to perform the same task many times, perhaps on a different item each time. In this case, you want to read in data to each element of an array. You set i equal to 0, specify when the loop needs to stop, and how much to change i by each time. Then you can use i to refer to the element of the array on which you are currently working.

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    7
    thank you very much your help got me a very long way.

    However id hate to become a nuisance but i have another glaring problem that i cannot seem to overcome. I have another input file containing information in this fashion.

    123:Firstname:Lastname01:H:40.1:1

    the rest of the lines looking very similar to that...same format however different values. My problem is i cannot figuer out an fscanf that would copy each individual value bewteen the colons and place each into a repsective variable. I am unable to get it even close as a matter of fact and ive tried many many different variations on my idea however sadly have failed a thousand times already. If anyone knows how to get me out of my rut please help for i shall fail my class and have to pay large sums of money that i cannot afford

    anyhelp is greatly appreciated, thank you

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    sscanf is very similar to fscanf.
    Code:
    #include <stdio.h>
    
    struct record
    {
       int     i;
       char    first [ 20 ];
       char    last  [ 32 ];
       char    c;
       double  d;
       int     j;
    };
    
    int main(void)
    {
       const char text[] = "123:Firstname:Lastname01:H:40.1:1";
       struct record entry;
       if ( sscanf(text, "%d:%19[^:]:%31[^:]:%c:%lf:%d",
                   &entry.i, entry.first, entry.last,
                   &entry.c, &entry.d, &entry.j) == 6 )
       {
          printf("entry.i     = %d\n", entry.i);
          printf("entry.first = %s\n", entry.first);
          printf("entry.last  = %s\n", entry.last);
          printf("entry.c     = %c\n", entry.c);
          printf("entry.d     = %f\n", entry.d);
          printf("entry.j     = %d\n", entry.j);
       }
       return 0;
    }
    
    /* my output
    entry.i     = 123
    entry.first = Firstname
    entry.last  = Lastname01
    entry.c     = H
    entry.d     = 40.100000
    entry.j     = 1
    */
    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.*

  10. #10
    Registered User
    Join Date
    Dec 2004
    Posts
    7
    thank you very much for your very quick reply you are possibly saving my ass. Im going to try it right now...though i do not understand all the code you replied with i can see mostly what i need and im sure ill be able to improvise.

    Again thank you very much for your help.

  11. #11
    Registered User
    Join Date
    Dec 2004
    Posts
    7
    Nice, that worked beautifully. Thanks again. However just to jump the gun because i know that i will have another issue with this shortly so i might as well see if you guys could help me out with it. Okay....so now that ive been able to get my one line off of that input file, and place all the values and names etc, into there respective variables, what i need to do is take the information gathered off line one of the input file and do some minor calculations (which i can handle) and then output my findings into an output file (which i can try and figure out how to do on my own). HOWEVER after getting the first line of data, processing it, then reoutputting it to another file, i must go back and skip the first line in the previous input file doing to same thing for line two, taking all the values and names and placing them in the same variables to be processed and output. However i need to know how would i go about skipping the first line after ive already obtained and used all the information i needed from it, so i could get the info from line two and process it etc Then i would naturally need to skip line one and two next time over to get data from line three to be processed and output. I know im being a pest here, but i do need the help and i am very appreciative for all your time used to help me out. If my post is unclear or you dont completely understand what im asking for ill be sitting here waiting if someone needs calrification.

    thanks again
    Last edited by something___; 12-08-2004 at 01:06 PM.

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Do you mean that you have an input file that looks like the following?
    Code:
    123:Firstname:Lastname01:H:40.1:1
    456:Firstname:Lastname02:I:50.1:2
    789:Firstname:Lastname03:J:60.1:3
    And example 'processing' such as the output I've shown you would then want like this?
    Code:
    entry.i     = 123
    entry.first = Firstname
    entry.last  = Lastname01
    entry.c     = H
    entry.d     = 40.100000
    entry.j     = 1
    
    entry.i     = 456
    entry.first = Firstname
    entry.last  = Lastname02
    entry.c     = I
    entry.d     = 50.100000
    entry.j     = 2
    
    entry.i     = 789
    entry.first = Firstname
    entry.last  = Lastname03
    entry.c     = J
    entry.d     = 60.100000
    entry.j     = 3
    Then [edit]assuming you're using fscanf in place of the scanf[/edit] change the if to a while.
    Last edited by Dave_Sinkula; 12-08-2004 at 01:28 PM.
    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
    Dec 2004
    Posts
    7
    yes indeed that would seem to be the easiest way to do it however, much of what you typed i did not completely understand because it is boyond my knowledge unfortunetaly. The only thing i used was the "%d:%19[^:]:%31[^:]:%c:%lf:%d" and i placed all the values obtained for that fscanf and places those into individual variables i created. And yes the input file does look like that. Ummm i guess the best way to make this easier for you would be to post my code...though i dont know if youll understand it cuz i really suck at this...but ill give it a shot..


    ++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++
    #include <stdio.h>

    main()
    {

    FILE *fp;
    int day, month, year, i, empnum, hrlywage;
    double payrates[6], hrsorsalary;
    char lastname[], firstname[], emptype;

    printf("Please input the current month (eg. January=01): ");
    scanf("%d", &month);
    printf("Please input the current day of the month (eg. 6th=06): ");
    scanf("%d", &day);
    printf("Please input the current year: ");
    scanf("%d", &year);

    /*Code below SHOULD open the input file containing the payrate values,
    scan each value and place each into an individual element in the array
    payrates*/

    fp = fopen("a3pay.in", "r");
    for(i=1; i<6; ++i)
    {
    fscanf(fp, "%lf", &payrates[i]);
    }

    fclose(fp);

    fp = fopen("a3.in", "r");

    fscanf(fp, "%d:%19[^:]:%31[^:]:%c:%lf:%d", &empnum, firstname, lastname,
    &emptype, &hrsorsalary, &hrlywage);
    }
    ++++++++++++++++++++++++++++++++++++++++++++++++++ +++++
    okay so theres no code to actuallly process this information yet, however that isnt the issue at the moment. Right now what i need is a method of going back over the exact same input file, however instead of reading from line one and placing all of that info into the variables, i need it to skip line one, that by this time has already been processed and output, and go onto the second line placing those values into the variables used to store the values from the first line. then inturn the second lines values will be processed and output, where it will go back now skipping line 1 and 2 going on to line 3 taking those values and placing them into the variables.

    i know this is very tedious and whatnot and im sorry to be wasting your time, but your help is very beneficial to me and i would liek to give my thanks again for your help and any more help your will be willing to give me

  14. #14
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    while ( fscanf(fp, "%d:%19[^:]:%31[^:]:%c:%lf:%d", &empnum, firstname, lastname,
                   &emptype, &hrsorsalary, &hrlywage) == 6 )
    {
       /* do stuff */
    }
    And you should give the proper size to your strings.
    Code:
    char lastname[32], firstname[20];
    Please post code within [CODE][/CODE] tags.

    [edit]fscanf man page
    Last edited by Dave_Sinkula; 12-08-2004 at 01:56 PM.
    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.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A basic math programming question
    By hebali in forum C Programming
    Replies: 38
    Last Post: 02-25-2008, 03:18 PM
  2. Basic question about GSL ODE func RK4
    By cosmich in forum Game Programming
    Replies: 1
    Last Post: 05-07-2007, 02:27 AM
  3. Basic question about RK4
    By cosmich in forum C++ Programming
    Replies: 0
    Last Post: 05-07-2007, 02:24 AM
  4. A very basic question
    By AshFooYoung in forum C Programming
    Replies: 8
    Last Post: 10-07-2001, 03:37 PM

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