Help with removing \n from a string in C


 
Thread Tools Search this Thread
Top Forums Programming Help with removing \n from a string in C
# 1  
Old 02-09-2011
Error Help with removing \n from a string in C

Code:
void remove_new_line(char *s) {
    while (*s) {
        if (*s == '\n') {
            *s='\0';
        }
        s++;
    }
}
// i tried this code for removing a \n from a string but its not working

# 2  
Old 02-09-2011
Code looks ok...so only explanation is that s doesnt contain a newline.
# 3  
Old 02-09-2011
also after i do this, when i print it , nothing comes up, i dont want to create a new string to fix this, what is wrong?
# 4  
Old 02-09-2011
Quote:
Originally Posted by omega666
also after i do this, when i print it , nothing comes up, i dont want to create a new string to fix this, what is wrong?
Create a pointer variable and initialize it to s before entering the loop and print out t before returning from the function...
Code:
void remove_new_line(char *s) {
    char *t = s;
    while (*s) {
        if (*s == '\n') {
            *s='\0';
        }
        s++;
    }
    printf("string: t\n", t);
}

# 5  
Old 02-09-2011
also how do i find out the length of the string excluding the \n at the end and the \0 at the end.
i am using
Code:
int str_len (char *s) {
    int count = 0;
    while ((char) *s != '\n' && (char) *s != '\0') {
        printf("%c",*s);
        s++;
        count++;
    }
    return count;
}

but when getting the lengths of strings i got from fgets, it gives me an extra count for all words except the last word. what is wrong with this?
# 6  
Old 02-09-2011
Do a man on strlen.
# 7  
Old 02-09-2011
Code:
char line[11];
FILE *fr;
fr = fopen(argv[1], "r");
    while(fgets(line, 11, fr) != NULL) {
        printf("length = %d\n",strlen( line ));
        printf(line);
    }
 fclose(fr);

this is in the main function
and it reads from

helloa
bye
cake
portald
birthday
portale
hellob

this is the contents of the file
and the out put is
length = 8
helloa
length = 5
bye
length = 6
cake
length = 9
portald
length = 10
birthday
length = 9
portale
length = 6
hellob



so yeah something is still wrong, but what?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing string from CSV file by provide removal string from other file

What I need is to remove the text from Location_file.txt from each line matching all entries from Remove_location.txt Location_file.txt FlowPrePaid, h3nmg1cm2,Jamaica_MTAImageFileFlowPrePaid,h0nmg1cm1, Flow_BeatTest,FlowRockTest FlowNewTest,FlowNewTest,h0nmg1cm1 PartiallySubscribed,... (3 Replies)
Discussion started by: ketanraut
3 Replies

2. Shell Programming and Scripting

Removing only Prefix string (!)

Hello everyone, I want to remove only prefix ME_ from all the values that are present in the FILEA. Below code I'm using for this. sed 's/ME\_//g' FILEA > FILEB Using the above code, all ME_ values are getting removed from the file. But the problem here is I want to remove only Prefix ME_... (4 Replies)
Discussion started by: ed_9
4 Replies

3. Shell Programming and Scripting

Removing Lines that Contain a Certain String

I'm writing a UNIX script to interpret text files and search for certain strings to output and need to delete certain lines that contain characters that I would like removed from the file. The rest of the script is already completed and I have it output how I would like, but I cannot seem to get... (2 Replies)
Discussion started by: DetroitLolcat
2 Replies

4. Shell Programming and Scripting

Removing un-necessary start string

i have input in below form part=gfjhwhedaqh=ghdgwg^*^(G== i want to remove the starting "part=" and retain the remaining. please suggest (4 Replies)
Discussion started by: skyineyes
4 Replies

5. Shell Programming and Scripting

Removing last character of string

Hi , I have a file with the following contents in file test1.txt . AMY_MTT_240Y001,N60_PG2_10G001,A2H_P3H_10G002,7C7_7D7_NP1,A2E_PV0_10G002,L78_PG1_64S001,A2H_P2M_NP2,LDN_YSN_64S001,WV6_WYV_64... (5 Replies)
Discussion started by: kinny
5 Replies

6. Shell Programming and Scripting

Removing spaces from string

I want a user to be able to paste in a string like "01 3F 20 1F" and have the script reformat it to "013F201F" to pass it on to the next step. I was trying to figure it out with awk but wasnt working well. Never mind, found answer. did not know about tr :) (7 Replies)
Discussion started by: ippy98
7 Replies

7. UNIX for Advanced & Expert Users

Removing first character in a string

While writing a shell script i happen to store some value in a string. Lets say the value is 59788. Now in this script i want to get the value 9788 removing the first charater 5. The original string length usually remains constant. Is there a single line command to do this or any simple way to... (4 Replies)
Discussion started by: npn
4 Replies

8. Shell Programming and Scripting

Removing substring from a string

Hi. I want to remove a substring from a string. If I use the "tr" command, it doesn't seem to to what I want it to: echo './somestring.dat' | tr -d './' results in somestringdat I want it to remove only occurances of the string './', rather than the individual character. The result... (1 Reply)
Discussion started by: dmilks
1 Replies

9. Shell Programming and Scripting

Removing characters from a string

I need help to strip out the first two characters of the variable $FileName. Please help. FileName=`find . -mtime +0 -name '*'` Contents of variable $FileName: ./SRIZVI4.MCR_IDEAS_REPORT.LAST.052705.075405.csv I want to strip out "./" and place the contents in another variable. How do I... (3 Replies)
Discussion started by: mh53j_fe
3 Replies

10. Shell Programming and Scripting

Removing the last occurance of string

I have a small query. I have a file containing the following lines abcd<12></12>fdfgdf<12>sdfgfg<12> sdfsdf<12></12>ytunfg<12> hggfhf<12>rtysb<12>zdfgdfg<12> Now I wish to delete ONLY the last occurance of string <12> from every lines of code. That mease my final output will be like this:... (7 Replies)
Discussion started by: dkhanna01
7 Replies
Login or Register to Ask a Question