Strlen

This is a discussion on Strlen within the C Programming forums, part of the General Programming Boards category; Hello. When i am using i read text from file and when i use strlen function I always getting sting ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    58

    Strlen

    Hello. When i am using i read text from file and when i use strlen function I always getting sting length longer by 1. Is there any alternative way to count string length except -1 because I need to find shortest line in .txt file.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
        FILE *fp;
        char filename[50];
        char str[100];
        int a,min=0;
        printf("Enter file name \n");
        scanf("%s",&filename);
        if ((fp=fopen(filename,"r"))==NULL)
        {
            printf("Error \n");
            system("pause");
            exit(1);
        }
      while (!feof(fp))
        {
            fgets(str, 99, fp);
             a=(strlen(str));
             if (a=1)
             if (min=0)
             min=a;
             else
             if (min<a)
             min=a;
       }
        printf ("%d", min);
        system("pause");
    
    }
    of course this isn't complete code because i faced this problem.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,556
    > if (a=1)
    This is assignment, not comparison.

    > while (!feof(fp))
    See the FAQ on why using feof() to control a loop is bad.

    Typically, we do
    while ( fgets( str, sizeof(str), fp) != NULL )
    to read each line in turn, and exit when eof is reached.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    >A newline character makes fgets stop reading, but it is considered a valid character and therefore it is included in the string copied to str.

    in other words, strlen is counting the newline character at the end of each line. since each line has a newline, you can just use the length as is, or subtract 1 if you like. except the last line may not have one, so you might need to check the end of each line whether to subtract 1 or not.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strlen
    By veronicak5678 in forum C++ Programming
    Replies: 15
    Last Post: 02-22-2008, 11:24 PM
  2. strlen help
    By stewie1986 in forum C Programming
    Replies: 10
    Last Post: 12-04-2007, 11:15 AM
  3. strlen()
    By BoneXXX in forum C Programming
    Replies: 6
    Last Post: 06-17-2007, 01:32 AM
  4. strlen()
    By exoeight in forum C Programming
    Replies: 9
    Last Post: 04-01-2005, 09:18 AM
  5. Why O why, strlen?
    By Sebastiani in forum C Programming
    Replies: 11
    Last Post: 08-24-2001, 01:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21