how can i open a data file to run this program?

Discussion in 'Mac Programming' started by manee, Oct 9, 2011.

  1. manee, Oct 9, 2011
    Last edited by a moderator: Oct 9, 2011

    manee macrumors newbie

    Joined:
    Sep 5, 2011
    #1
    I need my program to read a text file and count the number of occurrences of the letter e,both lowercase and uppercase. I saved the text file on my desktop and named it "file1". But I dont know what to write on xcode to make it open the file.

    file1
    Now is the time
    for all good men
    to come to the aid
    of their party


    code:
    Code:
    #include <stdio.h>
    #define EOF -1
    main()
    {
    
        int count,letter;
        count=0;
        letter= getchar();
        
        while(letter!= EOF)
        {
            if(letter=='e')
                ++count;
            letter=getchar();
        }
        printf("freqiency %d\n",count);
        
    
    }
     
  2. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #2
  3. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #3
    Your program assumes it can read the data file from standard input. To be able to do that you would need to redirect standard input from the keyboard to the datafile. You cannot redirect standard input/output using XCode.

    You have 2 choices. Compile and run the program Terminal.app. Or use fopen to open the data file at the beginning of your program using an absolute path to the data file, change getchar to getc, and close the data file with fclose.
     
  4. manee thread starter macrumors newbie

    Joined:
    Sep 5, 2011
  5. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #5
    In which folder is the .c file stored and what is the name of the .c file?
     
  6. subsonix, Oct 9, 2011
    Last edited: Oct 9, 2011

    subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #6
    If you double clicked on a .c file and had it set up to open Xcode, you (at least in Xcode 3) can use Xcode as an editor. For your purposes it sounds like what you want to do instead of working with a full on project at this time. In terminal you can redirect a file to stdin which means you can get you current program to work with the file. There might be settings in Xcode to redirect files to stdin as well, but I have not looked into that at all.

    Edit: Actually you can set command line arguments in Xcode so you should be able to set the first argument to < file1 with a full path.
     
  7. manee thread starter macrumors newbie

    Joined:
    Sep 5, 2011
    #7
    this is all greek to me. My c file's name is secondpro.c. I was wondering if anyone can post the coding i need to run the c program on terminal. I opened a terminal and have no idea how to go on from that.
     
  8. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #8
    You don't need any coding. You just need to find the source file, or the compiled file named a.out which are in your xcode project folder.

    But, you can set up Xcode to accept arguments.

    Look at the link I gave in the first post, chown33 shows where you do it, there is a tab in that window named arguments.

    In Xcode: Product -> Edit Scheme... -> Run -> Arguments
     
  9. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #9
    But you can't redirect. Look carefully at the OP's code. It's reading from stdin. Clearly it's now apparent that changing the program to read from a file is beyond the OP at this stage.


    A folder is also called a directory.

    The first thing you need to do is change directory (cd) to the folder where the .c file is stored.

    1. Find the .c file in Finder.
    2. Open Terminal
    3. Type cd and then a space
    4. Drag the folder containing the .c file (not the .c file itself) from Finder to Terminal. This will insert the path of the folder into Terminal.
    5. Press return.

    You only need to do this once per Terminal session. Each time you edit the .c file you'll need to (re-)compile it before you can run it.

    You can compile and run your program will a command like this:
    Code:
    gcc -o secondpro secondpro.c && ./secondpro < ~/Desktop/file1
    
    This will compile secondpro.c and produce an executable file called secondpro in the same folder. Any compiler errors or warnings will print in Terminal. If there are errors, nothing further will happen. If there are no errors, your program will run with the file1 file on your desktop.

    This command reads as: compile (gcc) and output secondpro (-o secondpro) by reading the source file secondpro.c (secondpro.c). If and only if that succeeds (&&), run secondpro (./secondpro) by redirecting standard input to the file1 file on my desktop (< ~/Desktop/file1).
     
  10. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #10
    Yes, look carefully at my previous answer. Where I mentioned the argument should be: < file1

    It at least was possible in Xcode 3.


    Also, what do you think is simpler. Supply the arguments to Xcode or require the OP to learn a new development environment. Perhaps also learn ho how to use FILE, fopen and so on.
     
  11. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #11
    I didn't find that in your quoted post. But it's really early for me, my first coffee is still well off from taking effect.

    I didn't work in XCode 4. It comes into argv. Damn.
     
  12. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #12
    manee, your first post asks about reading a file but what you got currently assumes that the file is redirected to stdin. Is this what you want to do, or are you not aware of re-direction, which is a shell feature?

    If you want to just open and read a file in the program itself, you need to use fopen(), and fgetc would be the closest to what you got currently. The other thread shows you how you can open the file.
     
  13. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #13
    This is a clear case of where Xcode only gets in the way.

    I'd recommend doing everything straight from the Terminal for now.
     
  14. manee thread starter macrumors newbie

    Joined:
    Sep 5, 2011
    #14
    yay..it worked on the terminal..however im confused how to use the "fopen" command as i have never used it before..would you mind showing me how to use it??
    thanks
     
  15. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #15
    Code:
    #include <stdio.h>
    #include <errno.h>
    
    int main(int argc, char **argv)
    {
        FILE *fp = NULL;
        int c = 0;
        int count = 0;
    
        // checking for command line arguments
        if(argc < 2) {
            fprintf( stderr, "%s\n", "Missing argument");
            return 0;
        }
    
        // open the file
        fp = fopen(argv[1], "r");
        if(fp == NULL) {
            perror("");
            return errno;
        }
    
        // this is similar to what you had before
        while( (c = fgetc(fp)) != EOF ) {
            if(c == 'e') {
                count++;
            }
        }
    
        printf("e: %d\n", count);
    
    
        // close the file
        fclose(fp);
        return 0;
    }
    
    You should get a C book and read up a bit on the language, or google for file handling in C.
     
  16. lloyddean, Oct 9, 2011
    Last edited: Oct 9, 2011

    lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #16
    Why not!

    Someone else's homework I did but don't think I ever posted!

    Code:
    /* =============================================================================
     * File - main.c
     * -----------------------------------------------------------------------------
     * <http://forums.macrumors.com/showthread.php?t=907772>
    
     * cars.txt
    
    Mercury Sable 99942 1 18 2001 5 30 1991 16 12.5
    Mazda Navajo 123961 2 20 1993 6 15 1993 19.3 16.7
    Toyota Camry 8223 6 13 2009 4 12 2009 15 8.9
    
     */
    
    /* =============================================================================
     * -----------------------------------------------------------------------------
     */
    
    #include <ctype.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    /* =============================================================================
     * -----------------------------------------------------------------------------
     */
    
    struct auto_struct
    {
        char    make[15];
        char    model[30];
        
        int     mmon;
        int     mday;
        int     myear;
        
        int     pmon;
        int     pday;
        int     pyear;
        
        float   fuelcap;
        float   fuellevel;
        
        float   odometer;
    };
    
    typedef struct auto_struct  auto_t, * auto_ptr;
    
    
    /* =============================================================================
     * -----------------------------------------------------------------------------
     */
    
    void print_auto(FILE* pf, auto_t vehicle)
    {
        fprintf(pf, "%s %s %d %d %d %d %d %d %f %f %f\n", vehicle.make, vehicle.model, vehicle.mmon, vehicle.mday, vehicle.myear, vehicle.pmon, vehicle.pday, vehicle.pyear, vehicle.fuelcap, vehicle.fuellevel, vehicle.odometer);
    }
    
    
    /* =============================================================================
     * -----------------------------------------------------------------------------
     */
    
    int scan_auto(FILE* pf, auto_ptr p)
    {
        return fscanf(pf, "%s %s %d %d %d %d %d %d %f %f %f", p->make, p->model, &p->mmon, &p->mday, &p->myear, &p->pmon, &p->pday, &p->pyear, &p->fuelcap, &p->fuellevel, &p->odometer);
    }
    
    
    /* =============================================================================
     * -----------------------------------------------------------------------------
     */
    
    int main(int argc, const char* const argv[])
    {
        int     result;
    
        FILE*   pfSrc = 0;
        FILE*   pfDst = 0;
        auto_t  vehicle;
    
        result  = EXIT_FAILURE;
    
        pfSrc   = fopen("cars.txt", "r");
        if ( ! pfSrc )  { goto bail; }
    
        pfDst = fopen("output.txt", "w");
        if ( ! pfDst )  { goto bail; }
    
        while ( EOF != scan_auto(pfSrc, &vehicle) )
        {
            print_auto(pfDst, vehicle);
        }
            
        result = EXIT_SUCCESS;
    
    bail:    
        if ( pfDst )    { flose(pfDst); }
        if ( pfSrc )    { flose(pfSrc); }
    
        return result;
    }
    
     

Share This Page