Solved

File reading in unix

Posted on 2000-03-09
9
242 Views
Last Modified: 2010-04-21
Does Unix have any function that would read a line fron file, stripping the newline character?
0
Comment
Question by:apaazue
9 Comments
 

Author Comment

by:apaazue
ID: 2601078
Thanks
/apaazue
0
 
LVL 84

Expert Comment

by:ozo
ID: 2601269
#include <stdio.h>
char *gets (char *s);
0
 

Author Comment

by:apaazue
ID: 2602183
I don't think gets()function will do it , since it is not used for file reading. I think fgets function is used for file reading line by line,but it does not strip the newline character from the end of string.
0
 
LVL 84

Expert Comment

by:ozo
ID: 2603163
It can be used for files if the file is opened to stdin.
Or you could fgets and then remove the \n yourself, or
fscanf("%[^\n]"
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 11

Expert Comment

by:tel2
ID: 2604104
apaazue,

I usually do it using:
  tr -d "\12" <infile >outfile
ie: TRanslate -Deleting ASCII(12 (octal), ie: newline).

That makes outfile which = infile with no newlines.  Is that what you're wanting?
0
 

Author Comment

by:apaazue
ID: 2605384
Let me give some code here, so that it will help in exactly understanding what i want.
fgets(line,MAXBUFF,fd);
I am using the above function to read a file line by line. I am doing a Client/Server program. I have to send each line to server as message. every time i have to strip the newline character and send it. Is there any way or any function that would directly read a line from file stripping the '\n' character.
0
 
LVL 84

Accepted Solution

by:
ozo earned 25 total points
ID: 2605498
#define str(s) #s
#define xstr(s) str(s)
fscanf(fd,"%" xstr(MAXBUFF) "[^\n]",line);
/* but it may be easier to strip the '\n' character yourself */
strtok(line,"\n");
0
 
LVL 5

Expert Comment

by:paulqna
ID: 2606272
C programming is nice but has anyone here heard of awk gawk or sed??
0
 

Author Comment

by:apaazue
ID: 2606355
Thanks
/apaazue
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now