0

I want to write code were the user is asked to write the name of a file. Then I want to analyze the file's content for a symbol, let's say 'e'.

My problem is that I don't know how to start analyzing the file the correct way so that the content can be checked.

int main() {
    char c[1000], file_name[1000];
    int i;
    int s = 0;
    FILE *fp;

    printf("Enter the name of file you wish to see\n");
    gets(file_name);

    if ((fp = fopen(file_name, "r")) == NULL){
        printf("Error! opening file");
        exit(1);

    }

    if (fp) {
        while (fscanf(fp, "%s", c) != EOF) {
            printf("%s", c);
        }

        fclose(fp);

        for (i = 0; c[i] != '\0'; ++i) {
            puts(c);
            if (c[i] == 'e') {
                ++s;
            }
        }

        printf("\nWhite spaces: %d", s);
        _getche();
        return 0;
    }
}
5
  • 4
    by "write", do you mean "read"? Commented Dec 11, 2014 at 18:29
  • Well to save the contents of the file to a string so i can run c[i] Commented Dec 11, 2014 at 18:31
  • you will need a char* where you will store all the content. Then, inside your while loop, do strcat(myFullString, c); this will append the new content to the "myFullString" array and you will then be able to use is in your for loop Look at this example: programmingsimplified.com/c-program-concatenate-strings Commented Dec 11, 2014 at 18:34
  • I must add a char pointer if i understand correctly ? I am verry sorry i am verry new to C and programing at all, but i am very passionate about it. Commented Dec 11, 2014 at 18:37
  • Are you looking for fread() ? cplusplus.com/reference/cstdio/fread Try fread(c,sizeof(char),1000,fp); Remember that in this case, c is an array of char, not a string. If you want c to remain a string : fread(c,sizeof(char),999,fp);c[999]='\0'; Commented Dec 11, 2014 at 18:44

2 Answers 2

2
char line[512]; /*To fetch a line from file maximum of 512 char*/
rewind(fp);
memset(line,0,sizeof(line)); /*Initialize to NULL*/
while ( fgets(line, 512, fp ) && fp !=EOF)
{

/*Suppose u want to analyze string "WELL_DONE" in this fetched line.*/

  if(strstr(line,"WELL_DONE")!=NULL)
  {
    printf("\nFOUND KEYWOD!!\n");
  }
  memset(line,0,sizeof(line)); /*Initialize to null to fetch again*/
}
Sign up to request clarification or add additional context in comments.

Comments

1

If its just a symbol you're looking for, or a char, you can simply use getc() :

int c;
....
if (fp) {
    while ((c = getc(fp)) != EOF) {
        if (c == 'e') {
            // Do what you need
        }
    }

Or, alternatively, if it's a word you're looking for, fscanf() will do the job:

int c;
char symb[100];
char symbToFind[] = "watever";  // This is the word you're looking for
....
while ((c = fscanf(fp, %s, symb)) != EOF) {
    if (strcmp(symb, symbToFind) == 0) {  // strcmp will compare every word in the file
        // do whatever                    // to symbToFind
    }
}

These alternatives will allow you to search every char or string in the file, without having to save them as an array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.