Follow us on Twitter!
I'd prefer to die standing, than to live on my knees - Che Guevara
Wednesday, April 23, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 23
Guests Online: 20
Members Online: 3

Registered Members: 82885
Newest Member: ConiBE
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

readline char array limit in C


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 31-08-08 23:42
#include <stdio.h>

int main (void)

{
int i;
char line[81];
void readLine (char buffer[]);

for ( i = 0; i < 3; ++i )
{
readLine (line);
printf ("%s\n\n", line);
}

return 0;
}

void readLine (char buffer[] )
{
char character;
int i = 0;

do
{

character = getchar ();
buffer[i] = character;
++i;

}
while ( character != '\n' );

buffer[i-1] = '\0';

}


The problem is I want to set a limit on the array so if more than 81 characters are put in with no new line character, it will store the null character at buffer[80] and then display the 80 characters. I've tried several things, but I really need to go to dinner right now so if you want I can fill you guys in later with more detail. Thanks in advance for any help.

edit- Sorry I noticed a couple syntax errors from when I was copying the code down into this thread. Like I said I was in a hurry, but it's fixed now.

Edited by on 01-09-08 00:34
Author

RE: readline char array limit in C


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 31-08-08 23:46
Code

#include <stdio.h>

int main (void)

{
    int i;
    char line[81];
    void readLine (char buffer[]);
   
    for ( i = 0; i < 3; ++i )
    {
        readLine (line);
        printf ("%s\n\n", line);
        }
       
        return 0;
}

void readLine (char buffer[], )
     {
              char character;
              int i = 0;
             
              do
              {
                 
                  character = getchar ();
                 if(i<=80)
                 {
                         buffer[i] = character;
                          ++i;
                 }
                                 
                }
              while ( character != '\n' );
             
              {buffer[i-1] = '\0';
               
              }





How is that?

Edited by on 31-08-08 23:48
Author

RE: readline char array limit in C


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 01-09-08 00:18
Try this out, I didn't test it but the idea makes sense.

Code
void readLine (char *buffer)
{
     
char character;
int i = 0;

do
{
character = getchar();
buffer[i++] = character;
}
while ( character != '\n');

if ( i > 80 ) buffer[80] = '\0';

}




ok changed it a little

Edit 2 : you might try strncpy also




Edited by on 04-09-08 00:59
Author

RE: readline char array limit in C


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 01-09-08 08:34
Yes, thank you. The difference was I needed to put the if (i<=81) statement after character = getchar (). I was putting the if (i<=81) before that, which was causing some unwanted results.