Jump to content

C Programming


msp301
 Share

Recommended Posts

I am trying to find a way of being able to read in an input and get my program to identify when the end of a line takes place in some inputted text, but my code doesn't seem to identify the return character "\n" ... can anyone help on how I may achieve this or what I am doing wrong ??

Thanks again

int i;
char buffer[999];

scanf("%s",&buffer);
    
        for (i=0; i<999; i++)
            {
                 if (buffer[i]=='\n')
                  {
                    printf("\n\n End of Line \n\n");
                  }
            }

Link to comment
Share on other sites

Try using NULL. I tired to compile your program to help you, but I got a syntax error before string constant at this line: scanf("%s",&buffer);. Have you compiled this file yet? Hope this helps and good luck.

cheers,

Destro

Link to comment
Share on other sites

==, !=, etc are used to compare numbers in C. You're going to have to use strcmp in order to determine if the new character in the buffer is a newline.

Nope, thats not the problem, comparing characters with ==, != etc. is perfectly fine. You can't do it with strings because you'll compare the reference not the value of the string. Characters aren't stored by reference like strings because they are essential just numbers displayed in a different way.

Have you tried printing out all the characters and checking that a '\n' is there? Because if there isn't one there it obviously can't trigger the if statement. What system are you running it on as well, Windows and Unix systems differ in how they handle end of lines and input.

Link to comment
Share on other sites

Nope, thats not the problem, comparing characters with ==, != etc. is perfectly fine. You can't do it with strings because you'll compare the reference not the value of the string. Characters aren't stored by reference like strings because they are essential just numbers displayed in a different way.

Have you tried printing out all the characters and checking that a '\n' is there? Because if there isn't one there it obviously can't trigger the if statement. What system are you running it on as well, Windows and Unix systems differ in how they handle end of lines and input.

I've added to the program and I've had it outputting the value of 'i' (which will be the value of characters in the string anyway) and it returns the same value of characters as had been input; so i'm guessing it's discarding the '\n' and just reading it as null.

I'm using Mac OS and my program is meant to be designed towards Unix-based systems; I would be using 'getline' instead to get the functionality that I want, but the GNU Libraries are incompatible with Darwin at the moment ... so I'm trying to keep to standard C :)

int i;
char buffer[999];
int chars_read;

scanf("%s",&buffer);
    
        for (i=0; i<999; i++)
            {
                 if (buffer[i]==NULL)
                  {
                    printf("\nbuffer = %s",buffer);
                    printf("\ncharacters in string = %d",i);
                    printf("\n\nEnd of Line\n\n");
                    chars_read = i;
                  }
            }

Link to comment
Share on other sites

Hi, having briefly looked at the code in the first post I can't see anything wrong with it. However, what may be the problem is that scanf may not return '\n'

For instance, if you enter the word 'test' and then hit enter on the console, scanf may return {'t', 'e', 's', 't', '\0'} as oppose to { 't', 'e', 's', 't', '\n', '\0'}

Also your code is currently susceptible to a buffer overflow, what would happen if some one were to feed your program a string longer than 999 chars?

Link to comment
Share on other sites

The definition of what 'the end of a line' is is platform specific.

On UNIX it's specified by the presence of a LINE FEED character (0x0A).

On a Mac it's specified by the presence of a CARRIAGE RETURN character (0x0D)

And for shits and giggles, on Windows it's specified by the presence of a CARRIAGE RETURN character, followed by a LINE FEED character, typically abbreviated to CRLF.

Another problem with your code snippet is that scanf stops reading input once it reaches any whitespace. The characters produced by the RETURN key are considered whitespace, so once you hit the RETURN key, rather than finishing the line with whatever definition your platform has with '\n' it simply terminates the string with a '\0'.

What I think you want is something like this:

#include <stdio.h>

#define CR 0x0D
#define LF 0x0A

int main(void) {
    char *buffer = "I don't need a pass to pass this pass!\n\t-- Groo The Wanderer.";

    char *ptr = buffer;
    while ( *ptr != '\0') {
        switch (*ptr) {
            case CR:    /* Make sure that if this CR is followed by an LF,
                           that LF isn't considered a separate new line. */
                        if (*(ptr+1) == LF) ptr+=1;
            case LF:    printf("\n\n End of Line \n\n");
                        break; /* Don't show the EOL character(s) */
            default: printf("%c",*ptr);
        }
        ptr+=1;
    }
    /* Obviously reaching the end of the string constitutes the end of a line. */
    printf("\n\n End of Line \n\n");
    return 0;
}

Oh, and finally note that scanf is a dangerous function. Either specify the maximum length in the format ("%999s") to prevent overflowing the buffer or use a different function such as getc() or fgets(char *).

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...