Link to home
Start Free TrialLog in
Avatar of aplelois
aplelois

asked on

name list

hello,
I have a name list in a .txt file that contains around 2k names of pople and places
I would like to save only the ones that start with e and have only 7 letters
in it, how can I do this?
thanks
SOLUTION
Avatar of twobitadder
twobitadder

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of aplelois
aplelois

ASKER

im trying this but is not working

#include <stdio.h>

int main ()
{
      FILE * fp;
      fp = fopen ("my_file.txt", "w");
 char currentLine[MAX_LINE_SIZE+1];

while(fgets(currentLine, MAX_LINE_SIZE,inFile)!=NULL)
{
    if(  tolower((int)currentLine[0]) == 'e'  && strlen(currentLine)==7 )
        fprintf(outFile, "%s", currentLine);
}
      fclose (fp);
      return 0;
}
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Good catch KK!

Paul
>>Good catch KK!

thx :)
or if you don't need a program, use Perl:


perl -p -e  "if /^e/   ||   #$_ == 8"

Kelvin_King , Im using Dev C++ and im getting some errors with your code
What errors?
>> Kelvin_King , Im using Dev C++ and im getting some errors with your code

Could you post them here. Thanks.
11 e:\docume~1\tutori~1\c\namewi~1\a.c  parse error before `char'
13 e:\docume~1\tutori~1\c\namewi~1\a.c `currentLine' undeclared (first use in this function)
13 e:\docume~1\tutori~1\c\namewi~1\a.c (Each undeclared identifier is reported only once
13 e:\docume~1\tutori~1\c\namewi~1\a.c for each function it appears in.)
Try moving the declaration of `currentLine' to the beginning of main().
I think you are missing a semi colon after line 10.

Also, see if you can get use of a C Compiler like gcc. Just download cygwin.
>>6 e:\docume~1\tutori~1\c\namewi~1\a.c  declaration for parameter `currentLine' but no such parameter


#include <stdio.h>

#define MAX_LINE_SIZE 50

int main ()
char currentLine[MAX_LINE_SIZE+1];
{
     
   FILE * fp, *outFile;
   fp = fopen ("my_file.txt", "r");
   outFile = fopen("new_file.txt", "w");

   
   while(fgets(currentLine, MAX_LINE_SIZE, fp)!=NULL)
   {
    if(  tolower((int)currentLine[0]) == 'e' && strlen(currentLine)== 8 )
         fprintf(outFile, "%s", currentLine);
   }
      fclose (fp);
      fclose (outFile);
      return 0;
}
I meant INSIDE the beginning of main().
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
=D thanks it works.. so... who gets the points ?
Split up the points.
btw, having the shift the declaration up a few lines is really strange behaviour. You might wanna get a c compiler anyway. Try gcc.
>>btw, having the shift the declaration up a few lines is really strange behaviour ...
Actually, I should have spotted that. Remember that putting declarations just about anywhere is a C++ feature, C declarations MUST be after another declaration or after a '{' or in global scope.

Paul