Sponsored Content
Top Forums Programming stdout/stdin + flushing buffers Post 302241576 by JamesGoh on Monday 29th of September 2008 06:51:11 PM
Old 09-29-2008
Hi guys

Please find my code in the attachments

Ok the aim of my script is to do the following

a) Prompt the user to enter in a folder they wish to browse to

b) Rename the folder via the use of a prompt and a system() call to mv

c) Within the folder, replace all subfolder names containing a certain string with a new string

d) Copy the renamed folder and the modified subfolder names to an external location

e) At the destination, repeat c)

I am experiencing my prompting issue at b).

This is the line of code at b)

Code:
new_src = (char *)malloc(20);
printf("\nEnter new directory name: ");
scanf("%s",new_src);

After debugging, I have found that new_src contains a portion of the char *ptr value I entered into a), which then mysteriously gets printed out at the printf() in b) at the scanf() part


FYI I used multiple calls to printf("\n===================") yesterday before b) and it seemed to alleviate the issue, however this came from a trial and error, rather than a methodical approach

The entire script is in the word doc file. You can find the functions individually in the text files

Is this enough for you guys to work with ?
 

10 More Discussions You Might Find Interesting

1. Programming

C++ How to use pipe() & fork() with stdin and stdout to another program

Hi, Program A: uses pipe() I am able to read the stdout of PROGAM B (stdout got through system() command) into PROGRAM A using: * child -> dup2(fd, STDOUT_FILENO); -> execl("/path/PROGRAM B", "PROGRAM B", NULL); * parent -> char line; -> read(fd, line, 100); Question:... (2 Replies)
Discussion started by: vvaidyan
2 Replies

2. Shell Programming and Scripting

Wrapper script for image deployment - stdin/stdout - named pipes and the like

Hi everyone, first post here. Anyone who isn't interested in the background, press pagedown :). I sometimes need to make scripts for little things I need in the infrastructure at the company I work at. Currently I am trying to make a wrapper script for a proprietary image-deployment program.... (2 Replies)
Discussion started by: andreas.ericson
2 Replies

3. UNIX for Dummies Questions & Answers

Redirect stdin stdout to multiple files

Hi, i know how to a) redirect stdout and stderr to one file, b) and write to two files concurrently with same output using tee command Now, i want to do both the above together. I have a script and it should write both stdout and stderr in one file and also write the same content to... (8 Replies)
Discussion started by: ysrini
8 Replies

4. Shell Programming and Scripting

can't close stdin/stdout in shell

#!/bin/sh exec 0</dev/null exec 1>/dev/null ls -l /proc/self/fd >&2 produces total 0 lr-x------ 1 tyler users 64 Feb 18 10:38 0 -> /proc/7886/fd lrwx------ 1 tyler users 64 Feb 18 10:38 1 -> /dev/pts/4 lrwx------ 1 tyler users 64 Feb 18 10:38 2 -> /dev/pts/4 I've verified the shell is... (10 Replies)
Discussion started by: Corona688
10 Replies

5. Shell Programming and Scripting

Redirecting stdin/stdout to/from command from/to string

Hi, I am working on a project where I have to generate and execute nasm code on-the-fly. I generate the code in a file program.asm and then execute it.This output is to stdout which i redirect to an output file which i read back to compare results: system("nasm -f elf program.asm >... (5 Replies)
Discussion started by: doc_cypher
5 Replies

6. Programming

Controlling a child's stdin/stdout (not working with scp)

All, Ok...so I know I *should* be able to control a process's stdin and stdout from the parent by creating pipes and then dup'ing them in the child. And, this works with all "normal" programs that I've tried. Unfortunately, I want to intercept the stdin/out of the scp application and it seems... (9 Replies)
Discussion started by: DreamWarrior
9 Replies

7. Programming

read and write stdin/stdout in unix

Hi, i am using the below program to read from the standard input or to write to standard out put. i know that using highlevel functions this can be done better than what i have done here. i just want to know is there any other method by which i find the exact number of characters ( this... (3 Replies)
Discussion started by: MrUser
3 Replies

8. UNIX for Dummies Questions & Answers

STDIN and STDOUT

Hallo, i have a script like: if ;then echo "OK" else echo "ERROR $2 is missing" fi; if ;then touch $2 fi; if ;then cat $1 | grep xy > $2 (1 Reply)
Discussion started by: eightball
1 Replies

9. UNIX for Advanced & Expert Users

How to set font color for STDIN,STDOUT and STDERR?

I want to differentiate the STDOUT and STDERR messages in my terminal . If a script or command is printing a message in terminal I want to differentiate by colors, Is it possible ? Example: $date Wed Jul 27 12:36:50 IST 2011 $datee bash: datee: command not found $alias ls alias... (2 Replies)
Discussion started by: ungalnanban
2 Replies

10. Shell Programming and Scripting

[stdin / stdout] Strategies for redirecting outputs

Well.. let's say i need to write a pretty simple script. In my script i have 2 variables which can have value of 0 or 1. $VERBOSE $LOG I need to implement these cases: ($VERBOSE = 0 && $LOG = 0) => ONLY ERROR output (STDERR to console && STDOUT to /dev/null) ($VERBOSE = 1... (5 Replies)
Discussion started by: Marmz
5 Replies
FFLUSH(3P)						     POSIX Programmer's Manual							FFLUSH(3P)

PROLOG
This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the correspond- ing Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux. NAME
fflush -- flush a stream SYNOPSIS
#include <stdio.h> int fflush(FILE *stream); DESCRIPTION
The functionality described on this reference page is aligned with the ISO C standard. Any conflict between the requirements described here and the ISO C standard is unintentional. This volume of POSIX.1-2008 defers to the ISO C standard. If stream points to an output stream or an update stream in which the most recent operation was not input, fflush() shall cause any unwrit- ten data for that stream to be written to the file, and the last data modification and last file status change timestamps of the underlying file shall be marked for update. If stream is a null pointer, fflush() shall perform this flushing action on all streams for which the behavior is defined above. For a stream open for reading, if the file is not already at EOF, and the file is one capable of seeking, the file offset of the underlying open file description shall be set to the file position of the stream, and any characters pushed back onto the stream by ungetc() or ungetwc() that have not subsequently been read from the stream shall be discarded (without further changing the file offset). RETURN VALUE
Upon successful completion, fflush() shall return 0; otherwise, it shall set the error indicator for the stream, return EOF, and set errno to indicate the error. ERRORS
The fflush() function shall fail if: EAGAIN The O_NONBLOCK flag is set for the file descriptor underlying stream and the thread would be delayed in the write operation. EBADF The file descriptor underlying stream is not valid. EFBIG An attempt was made to write a file that exceeds the maximum file size. EFBIG An attempt was made to write a file that exceeds the file size limit of the process. EFBIG The file is a regular file and an attempt was made to write at or beyond the offset maximum associated with the corresponding stream. EINTR The fflush() function was interrupted by a signal. EIO The process is a member of a background process group attempting to write to its controlling terminal, TOSTOP is set, the calling thread is not blocking SIGTTOU, the process is not ignoring SIGTTOU, and the process group of the process is orphaned. This error may also be returned under implementation-defined conditions. ENOMEM The underlying stream was created by open_memstream() or open_wmemstream() and insufficient memory is available. ENOSPC There was no free space remaining on the device containing the file or in the buffer used by the fmemopen() function. EPIPE An attempt is made to write to a pipe or FIFO that is not open for reading by any process. A SIGPIPE signal shall also be sent to the thread. The fflush() function may fail if: ENXIO A request was made of a nonexistent device, or the request was outside the capabilities of the device. The following sections are informative. EXAMPLES
Sending Prompts to Standard Output The following example uses printf() calls to print a series of prompts for information the user must enter from standard input. The fflush() calls force the output to standard output. The fflush() function is used because standard output is usually buffered and the prompt may not immediately be printed on the output or terminal. The getline() function calls read strings from standard input and place the results in variables, for use later in the program. char *user; char *oldpasswd; char *newpasswd; ssize_t llen; size_t blen; struct termios term; tcflag_t saveflag; printf("User name: "); fflush(stdout); blen = 0; llen = getline(&user, &blen, stdin); user[llen-1] = 0; tcgetattr(fileno(stdin), &term); saveflag = term.c_lflag; term.c_lflag &= ~ECHO; tcsetattr(fileno(stdin), TCSANOW, &term); printf("Old password: "); fflush(stdout); blen = 0; llen = getline(&oldpasswd, &blen, stdin); oldpasswd[llen-1] = 0; printf(" New password: "); fflush(stdout); blen = 0; llen = getline(&newpasswd, &blen, stdin); newpasswd[llen-1] = 0; term.c_lflag = saveflag; tcsetattr(fileno(stdin), TCSANOW, &term); free(user); free(oldpasswd); free(newpasswd); APPLICATION USAGE
None. RATIONALE
Data buffered by the system may make determining the validity of the position of the current file descriptor impractical. Thus, enforcing the repositioning of the file descriptor after fflush() on streams open for read() is not mandated by POSIX.1-2008. FUTURE DIRECTIONS
None. SEE ALSO
Section 2.5, Standard I/O Streams, fmemopen(), getrlimit(), open_memstream(), ulimit() The Base Definitions volume of POSIX.1-2008, <stdio.h> COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1, 2013 Edition, Standard for Information Technol- ogy -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, Copyright (C) 2013 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. (This is POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Stan- dard is the referee document. The original Standard can be obtained online at http://www.unix.org/online.html . Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html . IEEE
/The Open Group 2013 FFLUSH(3P)
All times are GMT -4. The time now is 06:30 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy