fgets read file line with "\n" inside

Login or Register for Dates, Times and to Reply

Thread Tools Search this Thread
Top Forums Programming fgets read file line with "\n" inside
# 1  
fgets read file line with "\n" inside


I have a string like this,

char str[] ="This, a sample string.\\nThis is the second line, \\n \\n, we will have one blank line";

if I want to use strtok() to seperate the string, which token should I use?

I tried "\n", "\\n", either not working.

# 2  
You realize that fgets() stops at a newline, yes? You can't read multiple lines with it.

If that's supposed to be the letters \ and n, not a newline, then strtok() won't work on that either: strok needs single characters as separators, tell it to split on "\\n" and it will split on | or n.

---------- Post updated at 04:58 PM ---------- Previous update was at 04:52 PM ----------

How about this:

#include <stdio.h>
#include <string.h>

int main(void)
        char str[] ="This, a sample string.\\n"
                        "This is the second line, \\n"
                        " \\n"
                        ", we will have one blank line";
        char *buf=str, *tok;

        while(tok=strstr(buf, "\\n"))
                printf("<%s>\n", buf);
        printf("<%s>\n", buf);

Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #859
Difficulty: Medium
AJAX is designed for two-way client-server communications.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

Failure: if grep "$Var" "$line" inside while read line loop

Hi everybody, I am new at Unix/Bourne shell scripting and with my youngest experiences, I will not become very old with it :o My code: #!/bin/sh set -e set -u export IFS= optl="Optl" LOCSTORCLI="/opt/lsi/storcli/storcli" ($LOCSTORCLI /c0 /vall show | grep RAID | cut -d " "... (5 Replies)
Discussion started by: Subsonic66
5 Replies

3. Shell Programming and Scripting

Bash - How to do a "read -p" inside a while loop?

Hi there guys! I was trying to do: while read line; do if ; then read -p "Press Enter to continue..." cont=0 fi echo $line let cont++ done < file.txt However, I have read that the read -p would not work in a while loop... I was wondering if there is any other way to... (2 Replies)
Discussion started by: rplae
2 Replies

4. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing To e-mail address and column 3 contains cc e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

5. Shell Programming and Scripting

speeding up bash script with "while read line"

Hello everybody, I'm still slowly treading my way into bash scripting (without any prior programming experience) and hence my code is mostly what some might call "creative" if they meant well :D I have created a script that serves its purpose but it does so very slowly, since it needs to work... (4 Replies)
Discussion started by: origamisven
4 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. Shell Programming and Scripting

defining a variable using multiple "entries/columns" within a line using read

Hello All, I am trying to figure out how to use the read command. I have a txt file that has rows looking something like this: 1 2 3 4 5 6 7 8 9 where each number represents an entry of various characters deliminated by tabs. My goal is to set entries 2-7 as a variable/string that I... (3 Replies)
Discussion started by: Torinator
3 Replies

8. Shell Programming and Scripting

read -p "prompt text" foo say "read: bad option(s)" in Bourne-Shell

Hallo, i need a Prompting read in my script: read -p "Enter your command: " command But i always get this Error: -p: is not an identifier When I run these in c-shell i get this error /usr/bin/read: read: bad option(s) How can I use a Prompt in the read command? (9 Replies)
Discussion started by: wiseguy
9 Replies

9. Shell Programming and Scripting

script to read a line with spaces bet " " and write to a file

Hi, I need a command in UNIX KSH below is the description... MAPPING DESCRIPTION ="Test Mapping for the calid inputs" ISVALID ="YES" NAME ="m_test_xml" OBJECTVERSION ="1" VERSIONNUMBER ="1" unix ksh command to read the DESCRIPTION and write to a file Test Mapping for the calid inputs... (3 Replies)
Discussion started by: perlamohan
3 Replies

10. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

Featured Tech Videos