Sponsored Content
Homework and Emergencies Homework & Coursework Questions grep Simulation Using Only Unbuffered I/O - C Post 302514697 by Corona688 on Sunday 17th of April 2011 02:27:59 PM
Old 04-17-2011
read() doesn't add NULLs to the end of your array, so checking for NULL won't work. read() DOES return how many bytes it read, so you could just stop there instead, but --

read() doesn't stop at the end of a line, either. You should read one by one and check for newlines yourself. You could try something like this:

Code:
// reads a line of text in a manner sort of like fgets, and adds null.
// returns the # of bytes read, 0 means error.
int my_getline(int fd, char *buf, int max)
{
        int n;
        // read until end of file, or newline, or max
        for(n=0; n<(max-1); n++)
        {
                 ssize_t b=read(fd, buf+n, 1); // read one byte
                 if(b <= 0) // end of file or error
                 {
                         buf[n]='\0'; // add null
                         return(n);
                 }
                 if(buf[n] == '\n') // end of line
                 {
                         buf[n+1]='\0'; // add null AFTER \n
                         return(n);
                 }
        }

        buf[n]='\0';
        return(n);
}

This User Gave Thanks to Corona688 For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help capturing and reformatting buffered and unbuffered output

Ok, so I have a shell script which runs a specific command, and that command sends it's output to the display. At certain times, the command sends buffered output, and at other times, the command sends unbuffered output in the form of a % progress bar, so if I run the command, the output I receive... (0 Replies)
Discussion started by: vikingshelmut
0 Replies

2. Programming

unbuffered streams.

#include "../ourhdr.h" int main(void) { int c; char *buf; setvbuf(stdin,buf,_IONBF,10); setvbuf(stdout,buf,_IONBF,10); while((c=getc(stdin)) != EOF) { if(putc(c,stdout) == EOF) err_sys("output... (2 Replies)
Discussion started by: gandhevinod
2 Replies

3. Windows & DOS: Issues & Discussions

unix simulation

hello everybody.. im new to this forum.. i have sme basic knowledge about unix.. but not too much.. i would like to practice shell programs n perl using a unix simulator.. but then i don't know wht a unix simulator means? just a bald definition that it creates a unix working environment in windows... (5 Replies)
Discussion started by: esash
5 Replies

4. Solaris

Results Load Simulation

If i simulate a load on a solaris system to choke the system to 90% CPU usage .. what is likely to increase .. my systems fan rpms, cpu temperature, power drawn .. Can anybody tell me (4 Replies)
Discussion started by: fugitive
4 Replies

5. Shell Programming and Scripting

Remote simulation and 'at' command

Hey, Task seems to be quite easy, but I'm still a bit green in shell scripting. I hope you can help me a bit. I have to run some simulation at the distance by remote terminal. Normally when I'm working on the server directly I just type: mpirun -np 8 compressibleInterFoam -parallel > log.txt... (7 Replies)
Discussion started by: PiPrus
7 Replies

6. Programming

Simulation using C/C++ and Java

Hi, I'm just start to learning simulate a network protocol using C/C++ and Java from scratch. Actually, I want to make two nodes can communicate using TCP and UDP protocol, for example http connection and video streaming, respectively. Can anyone help me find several references or guidance... (0 Replies)
Discussion started by: nica
0 Replies

7. UNIX and Linux Applications

2D collision simulation-programming

Hello, everybody, I'm thankful for the great helps you gave during the past year. Now I'm facing a serious problem: I was obliged to write a 2D collision simulation applet, and my experience is only in C,C++,Intelx86 assembly. I have no experience in Java and the like, and I don't know... (2 Replies)
Discussion started by: JackCrital2005
2 Replies

8. UNIX for Dummies Questions & Answers

Simulation of an ATM interface

Hi guys, I recently discovered this problem and any help would be great. 1) Create a file in vi named Accounts_File with the following data: The user, the name, the pin number, current balance and transaction history. There are 3 users who need to be inputted with all their PIN, etc... (1 Reply)
Discussion started by: Jimmy_c
1 Replies

9. IP Networking

OLSR simulation in ns2

# Create the simulator object that we need in order to run NS set ns # Set the parameters that we will use for wireless communications set val(chan) Channel/WirelessChannel ;# channel type set val(prop) Propagation/TwoRayGround ;# radio-propagation model set... (0 Replies)
Discussion started by: amithkhandakar
0 Replies

10. Cybersecurity

DDoS Simulation Tools

are there any popular DDoS simulation tools to test my own infrastructure? Anyone tried to setup all these in AWS EC2? (1 Reply)
Discussion started by: boriskong
1 Replies
gets(3) 						     Library Functions Manual							   gets(3)

NAME
gets, fgets - Get a string from a stream LIBRARY
Standard C Library (libc.so, libc.a) SYNOPSIS
#include <stdio.h> char *gets( char *string); char *fgets( char *string, int n, FILE *stream); STANDARDS
Interfaces documented on this reference page conform to industry standards as follows: gets(), fgets(): XPG4, XPG4-UNIX Refer to the standards(5) reference page for more information about industry standards and associated tags. PARAMETERS
Points to a string to receive bytes. Specifies an upper bound on the number of bytes to read. Points to the FILE structure of an open file. DESCRIPTION
The gets() function reads bytes from the standard input stream, stdin, into the array pointed to by the string parameter. Data is read until a newline character is read or an end-of-file condition is encountered. If reading is stopped due to a newline character, the newline character is discarded and the string is terminated with a null byte. The fgets() function reads bytes from the data pointed to by the stream parameter into the array pointed to by the string parameter. Data is read until n-1 bytes have been read, until a newline character is read and transferred to string, or until an end-of-file condition is encountered. The string is then terminated with a null byte. NOTES
The gets() function does not check the input for a maximum size. Consequently, if more bytes are entered than will fit in the space allo- cated for the string parameter, gets() will write beyond the end of the allocated space, producing indeterminate results. To avoid this condition, use fgets() instead of gets(). RETURN VALUES
Upon successful completion, the gets() and fgets() functions return string. If the stream is at end-of-file, the end-of-file indicator for the stream is set and a null pointer is returned. If a read error occurs, the error indicator for the stream is set, a null pointer is returned, and errno is set to indicate the error. ERRORS
The fgets() and gets() functions set errno to the specified value for the following conditions: The O_NONBLOCK flag is set for the underly- ing stream and the process would be delayed by the read operation. The file descriptor underlying the stream is not a valid file descrip- tor or is not open for reading. The read operation was interrupted by a signal which was caught and no data was transferred. The call is attempting to read from the process's controlling terminal and either the process is ignoring or blocking the SIGTTIN signal or the process group is orphaned. Insufficient memory is available for the operation. The device associated with stream does not exist. RELATED INFORMATION
Functions: clearerr(3), feof(3), ferror(3), fgetws(3), fileno(3), fopen(3), fputws(3), fread(3), getc(3), getwc(3), puts(3), scanf(3) Standards: standards(5) delim off gets(3)
All times are GMT -4. The time now is 04:10 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy