• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 257
  • Last Modified:

File reading in unix

Does Unix have any function that would read a line fron file, stripping the newline character?
0
apaazue
Asked:
apaazue
1 Solution
 
apaazueAuthor Commented:
Thanks
/apaazue
0
 
ozoCommented:
#include <stdio.h>
char *gets (char *s);
0
 
apaazueAuthor Commented:
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
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
ozoCommented:
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
 
tel2Commented:
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
 
apaazueAuthor Commented:
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
 
ozoCommented:
#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
 
paulqnaCommented:
C programming is nice but has anyone here heard of awk gawk or sed??
0
 
apaazueAuthor Commented:
Thanks
/apaazue
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now