file select

This is a discussion on file select within the C Programming forums, part of the General Programming Boards category; hi ive written a program which so far reads in data from a file. Code: #include <stdio.h> #include <string.h> // ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    3

    file select

    hi
    ive written a program which so far reads in data from a file.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    // the number of ints for the fishers structure
    #define MAX 4
    
    struct fisher
    {
       char name[80];
       char address[90];
       char postcode[14];
       /*
        * the int will hold, firstly a competetitor number, then a number for each
        * competition weight in ounces
        */
       int compNumber [MAX];
    };
    
    FILE *fisher_get(FILE *file, struct fisher *fisher)
    {
       char line[80], *newline;
       int  i, stones, pounds, ounces;
       /*
        * Get the name.
        */
       if ( !fgets(fisher->name, sizeof fisher->name, file) )
       {
          return NULL;
       }
       newline = strchr(fisher->name, '\n');
       if ( newline )
       {
          *newline = '\0';
       }
       printf("fisher->name = \"%s\"\n", fisher->name);
       /*
        * Get the address and post code.
        */
       if ( fscanf(file, "%89[^.]. %13[^\n]%*c", fisher->address, fisher->postcode) != 2 )
       {
          return NULL;
       }
          printf("fisher->address = \"%s\"\n", fisher->address);
          printf("fisher->postcode = \"%s\"\n", fisher->postcode);
       /*
        * Read and discard next line (or do whatever with it).
        */
       fgets(line, sizeof line, file);
       /*
        * Read the stones, pounds, and ounces for each entry.
        */
       for (i = 0; i < 3; ++i)
       {
          printf("i = %d:\n", i);
          if ( fscanf(file, "%d %d %d%*c", &stones, &pounds, &ounces) != 3 )
          {
             return NULL;
          }
          printf(" stones = %d\n", stones);
          printf(" pounds = %d\n", pounds);
          printf(" ounces = %d\n", ounces);
       }
       return file;
    }
    
    int main()
    {
       FILE *file = fopen("autumn.txt", "r");
       if ( file != NULL )
       {
          int i;
          char line[80];
          struct fisher fisher;
          /*
           * Read and display first two lines.
           */
          for ( i = 0; i < 2; ++i )
          {
             fgets(line, sizeof line, file);
             fputs(line, stdout);
          }
          while ( fisher_get(file, &fisher) )
          {
          }
          fclose(file);
       }
       return 0;

    im just stuck on the best way to select which file the user wants to read, as i have more than one, one called autumn.txt and one called spring.txt.

    any help very much welcomed

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by EllieProgrammer
    hi
    ive written a program which so far reads in data from a file.

    <snip familiar code>

    im just stuck on the best way to select which file the user wants to read, as i have more than one, one called autumn.txt and one called spring.txt.

    any help very much welcomed
    FAQ > How do I... (Level 1) > Get a line of text from the user/keyboard (C)
    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
    Apr 2005
    Posts
    3

    yeh i know that, but im asking how do i get the:

    FILE *file = fopen("spring.txt", "r");

    so it can select the result of the inputed file name , instead of a hardcoded file

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    You can pass filename as a command line argument to the program or select a filename based on some descision criteria. Use that filename (string) in fopen() . For e.g

    Code:
    constant char *file1 = "abc.txt"
    constant char *file2 = "efg.txt"
    char *file;
    FILE *fp;
    
    if (condition 1 is true)
    {
        file = file1;
    } else {
        file = file2;
    }
    
    fp=fopen(file,"r");

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Or you do what Dave is implying.

    See around here there are a bunch of us who actually like to see if you can think at all for yourself. Thus, we give you a point in the right direction, and see where you end up. Most of you end up back here whining about getting lost, or that we're not helpful enough.

    However, there are those that can think for themselves, and they usually appreciate what we do, because rather than just handing out a candy coated answer, they get to figure it out for themselves. These are the people who you will lose out in your job interviews too.

    Anyway, back on topic...

    Dave was suggesting, by way of a FAQ *, that you prompt the user for the file name, and pass that to fopen. See? I didn't need him to do anything more than provide one line of text to figure out what he had in mind. I'm sure I'm not alone in this.

    But for those who are lost, you've now been shown a big flashing beacon telling you what to do.

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

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    3
    idiot

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    And closed.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  3. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 09:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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