~Help - File processing with user input~
Hi all
This may sound like a silly/simply question how do i create a file from user input e.g. i want the user to input a file name and that input creates a file with same name...
required output...
1.Input file name to process: xyz (this xyz is created)then
i want to make sure the entry is made, if blank output a message..
2. which line no. to enter the data into...
3. Enter text....prompt user to ok to continue if no quit
4.if yes enter the text into the line no. and display it ...
i would appreciate if someone can give a c program script to get me started, thanks in advance....i am new to c programming...thanks again...indy
~thanks prelude - more help please~
hi prelude
thanks for the reply, but this is what i want to do , sorry i did not explain properly...
i want to enabling a user to insert a line in a specific part of a text file, i.e.
- ask user the name of the file,
- ask user where (line number) s/he wants to insert the line
- ask user to provide the text for this line
You then insert this line into the specific part of the file and ask user whether s/he wants to continue or not.
i would appreciate any sort of code snippet/script, i have tommorrow to finish it ...waiting for reply....thanks again...indy
~ More help on my code prelude~
Hi there prelude...
thanks for the reply, heres my code so far, i want to incoperate my previous logic...i real sloww in getting it into script and i need more help...thanks...indy
Code:
#include "stdio.h"
#include "stdlib.h"
void main(void)
{
FILE *fp;
char fname[128];
printf("Enter filename: ");
gets(fname);
if((fp=fopen(fname, "r"))==NULL) {
printf("cannot open file\n");
exit(1);
}
fclose(fp);
}
~ More help !!..code not working~
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
thanks - sloww coder...need more help..
hi there
thanks walt p ...i bit confused and having problems with adding text for a line number...had a look at the faq/archive but still having problems, my code below, does ask for file name and line number to add text but does not insert the text to that line number, have used cput,cget to add text, where is it i am going wrong...now it does not compile either...sorry...thanks in advance...i am too sloww for this group...sorry
Code:
#include "stdio.h"
#include "stdlib.h"
void main(void)
{
FILE *fp;
char s[80];
int t;
char p[23];/* for text for line number.*/
s[0] = 20;
if ((fp = fopen("test", "w")) == NULL) {
printf("sorry...cannot open file.. !!\n");
exit(1);
}
printf("Enter file name & Line Number to insert data.. : ");
/* Read from keyboard. */
/*fscanf(stdin, "%s%d", s, &t);*/
fscanf(stdin, "%s%d", s, &t, "%p%d", p);
/* read text for the location.*/
cputs("Enter text for Line number to insert.. : ");
cgets(s);
cputs(&s[2]);
/*write to file.*/
fprintf(fp, "%s %d", s, t);
fclose(fp);
if((fp=fopen("test", "r")) == NULL){
printf("sorry...cannot open file.. !!\n");
exit(1);
/* read from a file.*/
fscanf(fp, "%s%d", s, &t);
/* print to screen.*/
fprintf(stdout, "%s %d", s, t, p);
}
thanks in advance...indy
~ sorry - was bit lost !!~
hi prelude...
thanks...but i am bit confused and have alot of versions , yes i have well butchered it u are right, could u please start me off again....heres what i want to do...
enabling a user to insert a line in a specific part of a text file, i.e.
1-ask user the name of the file,
2- ask user where (line number) s/he wants to insert the line
3.ask user to provide the text for this line
4.then insert this line into the specific part of the file and ask user whether s/he wants to continue or not.
thanks again...indy
~ new code ...from scratch...
hi prelude
this is what i have from scartch...
Code:
#include <stdio.h>
#define MAX 10
#define LEN 80
char text[MAX][LEN];
FILE *openinfile(void);
void main(void)
{
register int t,i,j;
int n,row,p,x;
char y[80];
FILE * fp;
n=0;
fp = openinfile();
for (row=0;row<10;row++)
{
fgets(text[row],LEN,fp);
}
fclose(fp);
for(i=0;i<MAX;i++)
{
for(j=0; text[i][j];j++) putchar(text[i][j]);
}
printf("Enter text line to edit : \n");
scanf("%d", &n);
printf("Line selected is : %s\n", text[n]);
printf("Enter text for line....\n");
gets(text[n]);
if(!*text[n]) gets(text[n]);
for(i=0;i<MAX;i++)
{
for(j=0; text[i][j];j++) putchar(text[i][j]);
}
}
FILE *openinfile(void)
{
FILE *fp;
char infile[25];
printf("Input filename : ");
gets(infile);
fp = fopen(infile, "r");
return fp;
}