• C

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
apleloisAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

twobitadderCommented:
if each is on a seperate line do something like:

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);
}


having fopened and fclosed the files. fgets fetches a line at a time, otherwise you need to read character at a time or split on whatever delimiter is between the entries using strtok
apleloisAuthor Commented:
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;
}
PaulCaswellCommented:
>>     fp = fopen ("my_file.txt", "w");
Use "r" not "w" because you are reading the file.

>>        fprintf(outFile, "%s", currentLine);
Try
fprintf(stdout, "%s", currentLine);

Paul
Kelvin_KingCommented:
this works:

#include <stdio.h>

#define MAX_LINE_SIZE 50

int main ()
{
     
   FILE * fp, *outFile;
   fp = fopen ("my_file.txt", "r");
   outFile = fopen("new_file.txt", "w");
   char currentLine[MAX_LINE_SIZE+1];
   
   while(fgets(currentLine, MAX_LINE_SIZE, fp)!=NULL)
   {
    if(  tolower((int)currentLine[0]) == 'e' && strlen(currentLine)== 8 )
    // I changed to 8 because there's also a '\n' character which is read in.
         fprintf(outFile, "%s", currentLine);
   }
      fclose (fp);
      fclose (outFile);
      return 0;
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PaulCaswellCommented:
Good catch KK!

Paul
Kelvin_KingCommented:
>>Good catch KK!

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


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

apleloisAuthor Commented:
Kelvin_King , Im using Dev C++ and im getting some errors with your code
Tim_UtschigCommented:
What errors?
Kelvin_KingCommented:
>> Kelvin_King , Im using Dev C++ and im getting some errors with your code

Could you post them here. Thanks.
apleloisAuthor Commented:
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.)
Tim_UtschigCommented:
Try moving the declaration of `currentLine' to the beginning of main().
Kelvin_KingCommented:
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.
apleloisAuthor Commented:
>>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;
}
Tim_UtschigCommented:
I meant INSIDE the beginning of main().
Tim_UtschigCommented:
Maybe I should just show you...

int main ()
{
   char currentLine[MAX_LINE_SIZE+1];    
   FILE * fp, *outFile;
apleloisAuthor Commented:
=D thanks it works.. so... who gets the points ?
Kelvin_KingCommented:
Split up the points.
Kelvin_KingCommented:
btw, having the shift the declaration up a few lines is really strange behaviour. You might wanna get a c compiler anyway. Try gcc.
PaulCaswellCommented:
>>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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.