reading newline

This is a discussion on reading newline within the C Programming forums, part of the General Programming Boards category; I would like to read a newline and store it in a variable. I tried reading a \n from a ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    41

    reading newline

    I would like to read a newline and store it in a variable.

    I tried reading a \n from a standard input. but instead of reading as a newline it read as "\" and also "n". How can i combine the two things into one character ??

  2. #2
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    read a newline... Use fgets, that picks up the \n.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Use fgets()

    This is what I typed

    After a call to fgets() with a suitable buffer, it will look like this
    char buff[] = "This is what I typed\n";
    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.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    41
    I would like to replace all the a's in the text file with \n and all the b's in the text file with \t. How can i archieve this ??
    coz my current code it only replace all the a's with ' and all the b's with \. every each of ' , \ , n , \ , t and ' are being read in as character instead of just \n and also \t. I will appreciate if some one is able to modify the code so that it is able to read in \n as a character and \t as a character so that both the character are able to replace the a's and b's in the text file.

    testing '\n\t' < textfile.TXT

    File name: testing.c
    Code:
    main(int argc,  char *argv[])
    {
    char sentence[330];
    char c;
    int i=0;
    
    while(( c = getchar()) != EOF){
    	   sentence[i++] = c;
    }
    sentence[i] = '\0';
    
    printf("%s",replaceChar(sentence, argv[1]));
    
    }
    
    
    
    	
    char *replaceChar(char *sN, char *rC)
    {
    int new_len,i;
    
    	new_len = strlen(sN);
                    for(i=0; i< new_len; i++)
                    {
                                if(*sN == 'a')
                                            *sN = *rC;
                                else if(*sN == 'b')
                                {
                                         rC++;
                                         *sN = *rC;
                                         rC--;
                                }
    							sN++;
    
                    }
    
    
                    for(i=0; i< new_len; i++)
    	{
                    	sN--;
    	}
    	return sN;
    }
    Last edited by winsonlee; 03-19-2004 at 08:07 AM.

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Originally posted by winsonlee
    I would like to replace all the a's in the text file with \n and all the b's in the text file with \t. How can i archieve this ??
    coz my current code it only replace all the a's with ' and all the b's with \. every each of ' , \ , n , \ , t and ' are being read in as character instead of just \n and also \t. I will appreciate if some one is able to modify the code so that it is able to read in \n as a character and \t as a character so that both the character are able to replace the a's and b's in the text file.

    testing '\n\t' < textfile.TXT
    Format your code properly. Don't use TABs:
    Code:
    int main(int argc,  char *argv[])
    {
        char sentence[330];
        char c;
        int i=0;
    
        while(( c = getchar()) != EOF)
        {
            sentence[i++] = c;
        }
        sentence[i] = '\0';
    
        printf("%s",replaceChar(sentence, argv[1]));
        return 0;
    }
    	
    char *replaceChar(char *sN, char *rC)
    {
        int new_len,i;
    
        new_len = strlen(sN);
        for(i=0; i< new_len; i++)
        {
            if(*sN == 'a')
                    *sN = *rC;
            else if(*sN == 'b')
            {
                rC++;
                *sN = *rC;
                rC--;
            }
        sN++;
        }
    
        for(i=0; i< new_len; i++)
        {
            sN--;
        }
        return sN;
    }
    The problem is from the commande line the values in
    testing '\n\t' < textfile.TXT
    are characters ', \, n, and t. From the command line you cannot pass in a newline and tab. In your main() you'd have to parse your argv[1] and replace the characters \ & n with the single character '\n'
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-28-2008, 02:07 AM
  2. help with reading from stream
    By movl0x1 in forum C Programming
    Replies: 7
    Last Post: 05-31-2007, 10:36 PM
  3. Fun with reading hex
    By dpro in forum C++ Programming
    Replies: 7
    Last Post: 02-17-2006, 05:41 PM
  4. How to get rid of newline character
    By C++angel in forum C++ Programming
    Replies: 3
    Last Post: 02-07-2006, 06:50 PM
  5. reading file word by word
    By 98holb in forum C Programming
    Replies: 2
    Last Post: 01-25-2006, 04:49 PM

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