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
# 8  
Old 02-09-2011
You may have hidden characters in the input file...do a cat -vet filename and post the outputhere...
Code:
cat -vet file

# 9  
Old 02-09-2011
cat -vet new


helloa^M$
bye^M$
cake^M$
portald^M$
birthday^M$
portale^M$
hellob
# 10  
Old 02-09-2011
Quote:
and the output 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?
The output is correct and as expected since the file appears to have been created on an MSDOS-like system, i.e. one that uses CRLF instead of LF to represent newlines. The only anomaly is the last line which does not have a newline.
# 11  
Old 02-09-2011
i figured out whats wrong, i ad to use the dos to unix command to make it work.
# 12  
Old 02-09-2011
Or you can do something the following:
Code:
#include <stdio.h>

void
remove_new_line(char *s) {

    char *t = s;
    int n = 0;

    while (*s) {
        if (*s == '\n' || *s == '\r') {
            *s='\0';
            break;
        }
        s++;
        n++;
    }

    printf("string: %s (%d)\n", t, n);
}


int
main(int argc, char *argv[])
{

    char line[11];
    FILE *fr;

    fr = fopen(argv[1], "r");

    while(fgets(line, 11, fr) != NULL) {
        remove_new_line(line);
    }

    fclose(fr);
}

which gives the following output:
Code:
string: helloa (6)
string: bye (3)
string: cake (4)
string: portald (7)
string: birthday (8)
string: portale (7)
string: hellob (6)

# 13  
Old 02-10-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?
If there's a newline at the beginning of the string, it will eat it too, truncating the entire thing.
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