0

I need to find the number of lines in file which started with spaces. The following is the idea I had but it doesn't seem to work:

void SearchSymbolInLine(FILE *fp);
int main() 
{
    FILE * pFile;   
    char arr[80]={0};
    char c[80]={0};

    fflush(stdin);
    printf("Enter the file name:\n");
    gets(c);


    if(!(fp=fopen(fileName, "rt")))
    {
        printf("The file was not found\n");
    }
    else
    {
        SearchSymbolInLine(fp);
    }
    return 0;
}

void SearchSymbolInLine(FILE *fp)
{   
    char line[100];
    int ch=0;   
    int countA=0;

    while ((fgets(line,99,fp))!=NULL)
    {
        if(line[0]==' ')
        {
            countA++;
        }
    }

    printf("a= %d\n", countA);          
}

Please help. I do not know whether it should be read line by line to obtain. As far as I can tell I think the problem is that I can't limited review in the first character of the line.And I can't distinguish lines. I guess it would work with reading line by line but it is really complex. I think that there is a simpler way.

J.Brown
  • 1
  • 2
  • Possible duplicate of [C read file line by line](http://stackoverflow.com/questions/3501338/c-read-file-line-by-line) – Dan Field Dec 30 '15 at 13:33
  • This likely is a duplicate of http://stackoverflow.com/questions/3501338/c-read-file-line-by-line. Why are you assuming that the file lines will only ever be 100 chars wide? You're quite possibly reading more or less than a single line of a file. – Dan Field Dec 30 '15 at 13:33
  • What do you mean by "doesn't seem to work"? You need to be more specific if you expect any help. – Andrew Henle Dec 30 '15 at 13:34
  • With "it doesn't seem to work" I mean that it counts incorectly. Actually I'm not sure why I wrote char line[100]. I'm traing to do this all day and now in my head is totally mess. – J.Brown Dec 30 '15 at 13:38

0 Answers0