Inputting Data File Using Pointers

This is a discussion on Inputting Data File Using Pointers within the C Programming forums, part of the General Programming Boards category; I have a set of scores in a data file and I am having trouble figuring out the correct code ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    5

    Inputting Data File Using Pointers

    I have a set of scores in a data file and I am having trouble figuring out the correct code to input the scores from the data file into a separate function. Is this a good start?


    void get_data(int ar[])
    {
    int i;
    FILE *inp,
    *outp;

    inp = fopen("proj7.dat", "r");
    outp = fopen("proj7.dat", "w");


    I have to open the file, read it (using a While loop) and then close the file.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Are you really trying to read and write to the same file at the same time?

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    5
    The function has to read (using a while loop) 15 scores from a separate data file using the declaration of a file pointer and I was unsure of the syntax to do so.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, the syntax for the open is right then. But you won't be able to write to the same data file (I don't know if you'd be able to open the same file twice, but nothing good can come of it). If you need to write to a file, write to a different file.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I would say do not open the "out" file in your get_data() function. It looks like you intend to copy the data into an int array? Anyway, get_data() should just open the data file, read the data, store it for use elsewhere in the program, and close the file.

    You can write out to the other file in a separate function. It is much nicer and easier to use a collection of short, simple functions than one long meandering one.

    What does the file look like? If you are retrieving scores, I would recommend using fgets() to read line by line, and process the line *inside* the while loop using sscanf() or strtok(). For that, you will need a char "buffer" to read the line into. For example:
    Code:
    int i=0; /* a counter */
    char buffer[1024];  /* should be long enough */
    while (fgets(buffer,1024,1,inp)) {
          sscanf(buffer, "%d", &ar[i]);
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    5
    okay that is much more helpful!


    Void function A has to open a separate data file, read it, and then close it. (15 #'s to read)

    Void function B then computes the average of these 15 numbers.


    Its these two separate void functions I am having trouble writing.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by jl864405 View Post
    Its these two separate void functions I am having trouble writing.
    Very Important: Do not even bother thinking about the second one until you have completed the first one.

    So, write a program with one function besides main() -- get_data(). Call that function from main, and have it write the 15 scores into the int array. Then, print the array out in main():
    Code:
    void get_data(int ar[]);
    int main() {
           int i; /* another 'i'terative counter */
           int scores[15];
           get_data(scores);
           for (i=0;i<15;i++) printf("%d\t",scores[i]);
           return 0;
    }
    Once you have that working, so that it compiles without errors or warnings, etc. it will be dead simple to add the second part.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    Jun 2009
    Posts
    5
    So sorry for jumping ahead. Heres my get_data function so far:



    void get_data(int ar[])
    {
    FILE *inp;

    inp = fopen("proj7.dat", "r");
    fclose(inp);




    So I have declared the pointer file to store the data in (inp), opened the file (fopen), read the file("r") and closed the file (fclose). My question is how can I use a while loop to read the values within the file that has 15 numbers in it?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You declared the pointer, opened the file, and closed the file.

    What you haven't done is read the file.

    You should look at fscanf and friends to do so.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Replies: 3
    Last Post: 02-26-2008, 01:12 PM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. Please Help Reading Data From A File
    By NickHolmes in forum C Programming
    Replies: 5
    Last Post: 05-29-2005, 11:24 PM
  5. spell check in C using a dictionary file
    By goron350 in forum C Programming
    Replies: 10
    Last Post: 11-25-2004, 05:44 PM

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