RocktheFries
RocktheFries

Reputation: 221

Trying to fprintf a certain string from a file read

I am struggling to print a string from a file, to a new file and cant seem to wrap my head around it nor get it to work, any help would be great.

The file looks like this:

New York,4:20,3:03
Kansas City,12:03,3:00
North Bay,16:00,0:20
Kapuskasing,10:00,4:02
Thunder Bay,0:32,0:31

I am trying to fprintf just the file names to a new file called theCities.txt. The logic makes sense in my head but in terms of implementation, I don't know how I can fprintf a pointer to the string. Any help would be great.

while (fgets(flightInfo[i], 1024, fp) > 0) {
    clearTrailingCarraigeReturn(flightInfo[i]);
    // display the line we got from the fill
    printf("  >>> read record [%s]\n", flightInfo[i]);

    char *p = flightInfo[i];
    for (;;) {
        p = strchr(p, ',');
        fp = fopen("theCities.txt", "w+");
        fprintf(fp, "%s\n", p);
        if (!p)
            break;
        ++p;
    }
    i++;
}

Upvotes: 0

Views: 49

Answers (3)

RocktheFries
RocktheFries

Reputation: 221

Just to update as to what I did, I used the method from @Paul Ogilvie and adjusted it so I can still take a cmd line argument of a file path/name. I then used strtok instead and skipped over the numbers, so it only outputted the City Names to the txt file. Thank you all for your help!

FILE *fpIn, *fpOut;
if (!(fpIn = fopen(argv[1], "r"))) return -1;
if (!(fpOut = fopen("theCities.txt", "w+"))) { fclose(fpIn); return -1; }
while (fgets(flightInfo[i], 1024, fpIn) > 0)
{
    clearTrailingCarraigeReturn(flightInfo[i]);
    // display the line we got from the fill
    printf("  >>> read record [%s]\n", flightInfo[i]);

    char *p = flightInfo[i];
    char *n = flightInfo[i];
    char *c = flightInfo[i];
    while(p != NULL)
    {
        p = strtok(p, ",");
        n = strtok(p, "[1-9]");
        c = strtok(p, ":");
        if (!p) break;

        while (n != NULL && c != NULL)
        {
            n = strtok(NULL, " ");
            c = strtok(NULL, " ");
        }
        fprintf(fpOut, "%s\n", p);
        p++;
        p = strtok(NULL, " ");
    }
    i++;

}
fclose(fpIn);
fclose(fpOut);

Upvotes: 0

chqrlie
chqrlie

Reputation: 145109

Your code has problems:

  • you redefine variable fp in a nested scope, which is very confusing.
  • you should open the output file just once before the loop.
  • you should use "%.*s" to output a portion of the string instead of the end of the line.

Here is a modified version:

FILE *outp = fopen("theCities.txt", "w");
for (i = 0; fgets(flightInfo[i], 1024, fp) > 0; i++) {
    clearTrailingCarraigeReturn(flightInfo[i]);
    // display the line we got from the fill
    printf("  >>> read record [%s]\n", flightInfo[i]);

    int city_length = strcspn(flightInfo[i], ",");
    if (city_length) {
        fprintf(outp, "%.*s\n", city_length, flightInfo[i]);
    }
}
fclose(outp);

Upvotes: 0

Paul Ogilvie
Paul Ogilvie

Reputation: 25286

You are handling your file pointers wrong:

FILE *fpIn, *fpOut;
if (!(fpIn= fopen("yourfile.txt", "r"))) return -1;
if (!(fpOut=fopen("cities.txt", "w"))){fclose(fpIn); return -1;}
while (fgets(flightInfo[i], 1024, fpIn) > 0)
{
    clearTrailingCarraigeReturn(flightInfo[i]);
    // display the line we got from the fill
    printf("  >>> read record [%s]\n", flightInfo[i]);

    char *p = flightInfo[i];
    for (;;)
    {
        p = strchr(p, ',');
        fprintf(fpOut, "%s\n", p);
        if (!p) break;
        ++p;
    }

    i++;

}
fclose(fpIn);
fclose(fpOut);

Upvotes: 1

Related Questions