Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

manee

macrumors newbie
Original poster
Sep 5, 2011
6
0
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);
    

}
 
Last edited by a moderator:
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.
 
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.
 
Last edited:
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.
 
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.

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
 
But, you can set up Xcode to accept arguments.

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.


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.

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).
 
But you can't redirect. Look carefully at the OP's code.

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.
 
Yes, look carefully at my previous answer. Where I mentioned the argument should be: < file1

It at least was possible in Xcode 3.

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.
 
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.
 
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
 
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

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.
 
Why not!

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

Code:
/* =============================================================================
 * File - main.c
 * -----------------------------------------------------------------------------
 * <https://forums.macrumors.com/threads/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;
}
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.