Solved

File reading in unix

Posted on 2000-03-09
9
248 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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
 
LVL 12

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
llcommand 6 91
Host issue and need to move VM's 2 274
LINUX : Create user with required privilages to take backup of a file system 5 74
NTP Server in VMware 5 212
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…
This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…

775 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