Strlen( ) function

This is a discussion on Strlen( ) function within the C Programming forums, part of the General Programming Boards category; Ok i created this little dumb prog.. that will output text from a text file.. but.. there is one thing.. ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    227

    Strlen( ) function

    Ok i created this little dumb prog.. that will output text from a text file.. but.. there is one thing.. the text goes all accross the screen
    and i want it so that on a certain char it will just go to the next like i tried it.. but i keeped getting errors =(

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main( )
    {
        char Filename[10] = "test.txt";
        char buff[356];
        int lenght;
        FILE *fp;
    
    
        if(( fp = fopen( Filename, "r" )) == NULL )
        {
    
             fprintf( stderr, "Could not open %s for reading", Filename );
             exit( 1 );
    
        }
    
             fgets( buff, 356, fp );
             fprintf( stdout, " %s ", buff );
    
             getchar( );
             return 0;
    
    
    }
    Only the strong survives.

  2. #2
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    If you are not searching for a specific character and just a certain number of characters per line, then use 2 for loops to read each character and the first thing outside the inner for loop will be the newline. Then the outer for loop will start the inner for loop again reading characters until that number is hit again, and again, and again, blah blah blah blah.
    The keyboard is the standard device used to cause computer errors!

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    227
    But wouldn't the for loop just print everything out.. heh i tried it and it gave me an illegal operation =/ can you show me an example on adding the for loop in conjunction to the fgets or stdout

    this is how i first did it

    Code:
    fgets( buff, 356, fp );
          for( l = 0; l < 10; l++ )
          {
             fprintf( stdout, " %s ", l[buff]  );
          }

    tring it with one loop to see if it works
    Last edited by xlordt; 09-02-2003 at 03:41 AM.
    Only the strong survives.

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    227
    ok i done that now all i get is...

    t t t t t t t t t t t t t t t t t t t t

    instead of it.. giving me just upto the tenth char.. check it... this is what i done

    Code:
      if(( fp = fopen( Filename, "r" )) == NULL )
        {
    
             fprintf( stderr, "Could not open %s for reading", Filename );
             exit( 1 );
    
        }
    
    
          for( l = 0; l < 10; l++ )
          {
             fgets( buff, 256, fp );
             fprintf( stdout, "%c ",buff[i]  );
          }
    
             getchar( );
             return 0;
    
    
    }

    or do i really need to add the second for look at this moment for it to work? im tring to understand abit here

    btw this is what i have in the file that it opens

    this is a test of the emergency braudcast system. this is a test and only a test and no action should be taken on your part
    Only the strong survives.

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    You forgot:
    Code:
          for( l = 0; l < 10; l++ )
          {
             fgets( buff, 256, fp );
             for (i=0; i<strlen(buff); i++)
             {
                 fprintf( stdout, "%c ",buff[ i]  );
    
             } 
          }
    Yes, you need the second loop for each character of buff
    Last edited by WaltP; 09-03-2003 at 09:06 PM.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>fprintf( stdout, "%c ",buff[ i] );
    Or simply:
    putchar(buff[i]);
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >for (i=0; i<strlen(buff); i++)
    Slight nitpick, but calling strlen as the loop condition can create performance problems. It's just as easy to stick the result in a variable and just retrieve it:
    Code:
    for (l = 0; l < 10; l++)
    {
      int blen;
    
      fgets(buff, 256, fp);
      blen = strlen(buff);
      for (i = 0; i < blen; i++)
        putchar(buff[i]);
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 06:05 PM

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