Hi ther quaze
thanks for the reply,hint...much appreciated, i have the code below which i want to modify so that i can prompt the user with the following while reading a file into memory..
1. enabling a user to insert a line in a specific part of a text file, i.e.
- prompt user the name of the file,
- prompt user where (line number) s/he wants to insert the line
- prompt user to provide the text for this line
then i want to insert this line into the specific part of the file and ask user whether s/he wants to continue or not.
the code below works but only prompts the user new number to be added, adds it and lists the contents of the whole file...how can i modify it to ask for the above user inputs...feel free to modify ...thanks in advance...indy
Code:
#include <stdio.h>
#include <stdlib.h>
/* Define the maximum value e.g. 20 in this case.*/
#define MAX 20
int main ( void )
/* File/Integer declarations.*/
{
FILE *io;
int file[BUFSIZ];
int i, j;
int new_item;
/* Reading the input file with read mode.*/
io = fopen ( "file.txt", "r" );
/* Open file failure error routines.*/
if ( io == NULL ) {
perror ( "File open failure" );
return EXIT_FAILURE;
}
i = 0;
while ( fscanf ( io, "%d%*c", &file[i] ) == 1 )
i++;
fclose ( io );
/* User input declaration.*/
printf ( "Enter a new number to be added to the file..: " );
fflush ( stdout );
/* Output Buffer setup.*/
if ( scanf ( " %d", &new_item ) != 1 ) {
fprintf ( stderr, "Sorry Invalid input, please try again..!!" );
return EXIT_FAILURE;
}
io = fopen ( "file.txt", "w" );
if ( io == NULL ) {
perror ( "File open failure" );
return EXIT_FAILURE;
/* New entry input & modifications and output to screen.*/
}
for ( j = 0; j < i && file[j] <= new_item; j++ ) {
fprintf ( io, "%d,", file[j] );
printf ( "%d,", file[j] );
}
fprintf ( io, "%d,", new_item );
printf ( "%d,", new_item );
for ( ; j < i; j++ ) {
fprintf ( io, "%d,", file[j] );
printf ( "%d,", file[j] );
}
fclose ( io );
return EXIT_SUCCESS;
}
thanks again...indy