4 Replies - 290 Views - Last Post: 28 March 2013 - 12:03 PM Rate Topic: -----

#1 hamaada  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 3
  • Joined: 21-March 13

user to input filename and text

Posted 21 March 2013 - 03:54 PM

I want the user to input file name and text this is my code and it only makes an empty file with garbage name,
help pleaase.
#include <stdio.h>

int main(void)
{
    char fname[20];
	char text[30];
    FILE * myfile;
	printf("Enter File name");
	
    myfile = fopen(fname,"w");
	gets(fname);
    printf("Enter you text here");
	gets(text);
	
    fclose(myfile);
    
    return 0;
}


*** EDIT ***

Please use code tags when posting code.
:code:

This post has been edited by GunnerInc: 21 March 2013 - 03:56 PM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: user to input filename and text

#2 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,277
  • Joined: 28-March 11

Re: user to input filename and text

Posted 21 March 2013 - 04:01 PM

Shouldn't this:
    myfile = fopen(fname,"w");
	gets(fname);


Be this:
	gets(fname);
    myfile = fopen(fname,"w");

After all you want to "get" the filename from the user before you open the file, right?

The file is empty because you get the text to add to the file, but never write it to file.

Also, I would use fgets instead of gets, since gets can overflow your buffer.
Was This Post Helpful? 1
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon


Reputation: 3989
  • View blog
  • Posts: 12,307
  • Joined: 25-December 09

Re: user to input filename and text

Posted 21 March 2013 - 04:09 PM

Quote

Be this:

Close but this would be better:
fgets(fname, 20,stdin);
fname[strlen(fname)-1] = '\0'; // Remove end of line character
myfile = fopen(fname,"w");


Never use gets() or any other function that doesn't limit the number of characters that will be received. The gets() function is considered extremely unsafe.

Jim

This post has been edited by jimblumberg: 21 March 2013 - 04:09 PM

Was This Post Helpful? 1
  • +
  • -

#4 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,277
  • Joined: 28-March 11

Re: user to input filename and text

Posted 21 March 2013 - 04:12 PM

Off topic, but that is how I do it :punk:
    push    dword [stdin] 
    push    INPUT_MAX
    push    lpPalInput
    call    fgets
    add     esp, 4 * 3
    
    push    eax
    call    strlen
    add     esp, 4
    dec     eax
    mov     byte [lpPalInput + eax], 0

Hehehehe
Was This Post Helpful? 1
  • +
  • -

#5 hamaada  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 3
  • Joined: 21-March 13

Re: user to input filename and text

Posted 28 March 2013 - 12:03 PM

View Posthamaada, on 21 March 2013 - 03:54 PM, said:

I want the user to input file name and text this is my code and it only makes an empty file with garbage name,
help pleaase.
#include <stdio.h>

int main(void)
{
    char fname[20];
	char text[30];
    FILE * myfile;
	printf("Enter File name");
	
    myfile = fopen(fname,"w");
	gets(fname);
    printf("Enter you text here");
	gets(text);
	
    fclose(myfile);
    
    return 0;
}


*** EDIT ***

Please use code tags when posting code.
:code:/>


Thanks all, this code works perfect.
#include <stdio.h>
#include <stdlib.h>

int main(void)

{

    FILE * fp;
	char fname[9];
	char text[250];
  

	
	puts("Please enter file name max characters 8");
	gets(fname);
	puts("Please enter Text Min 64 and max 250");
	gets(text);
	if((fp = fopen(fname, "w"))==NULL);
	printf("Your file and text is saved\n");
	{
	fputs(text,fp);
	fclose(fp);
	}
	getchar();
   return 0;

}


Was This Post Helpful? -1
  • +
  • -

Page 1 of 1