Hello all,
Having a little trouble with this one. I don't really know C, so i'm not too sure what's going on. Trying to edit an existing program to do what is needed.
Have a file with:
hello
num123456
num1232
num09329823
goodbye
need to output to another file
123456
1232
09329823
pretty easy i'd imagine, but not for me. what i'm working with:
Code:#include <stdio.h> #include <stdlib.h> #include <string.h> void main(int arcg, char *argv[]) { char file1[20] = " "; char file2[20] = " "; char instring[85]; char volser[30] = " "; char *posch; FILE *infile, *outfile; int pos = 0; if (arcg != 3) {printf("\cut [file to scan] [file to write to]"); return; } strcpy(file1, argv[1]); strcpy(file2, argv[2]); if ((infile = fopen(file1,"r")) == NULL) { printf("The file that you want\n"); printf("scanned is not found. Try again."); return;} if ((outfile = fopen(file2,"w")) == NULL){ fclose(infile); return;} while (fscanf(infile,"%s", &instring) !=EOF) { if ( (posch = strstr( instring, "goodbye")) == NULL) { if ((posch = strstr( instring, "num")) != NULL) { strncpy(volser, &instring[15], (strlen(instring) - 0)); fprintf(outfile,"%s\n", volser); strcpy(volser, " "); strcpy(instring, " "); } } } fclose(outfile); fclose(infile); }
as you can tell i'm having much trouble, like i said, it was an existing piece of code that did something similiar. I don't know how to ignore the "hello" line, and don't understand how this would know to skip the "num" statements before the numbers.
I'd have to imagine there must be a better way of doing this.
Any help would be appreciated. (wouldn't mind an explination of what's going on above either )
Thanks