954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

File Processing

can i know how to save a file name based on the user's input

for example:

Enter your name : Derrick


then i save this input to a file .txt named derrick (derrick.txt)

how can i do it?

i only know how to use the rename() and delete() function...in the file processing

thank you for your kindness

koh
Junior Poster in Training
73 posts since Jul 2004
Reputation Points: 11
Solved Threads: 0
 

In C,

#include <stdio.h>

int main(void)
{
   char filename[256], name[] = "Derrick";
   FILE *file;
   sprintf(filename, "%s.txt", name);
   file = fopen(filename, "w");
   if ( file != NULL )
   {
      fprintf(file, "%s\n", name);
      fclose(file);
   }
   return 0;
}
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

in c++? is the syntax the same or different?

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

>in c++? is the syntax the same or different?

#include <iostream>
#include <string>

using namespace std

int main()
{
  string name = "Derrick";
  string filename = name + ".txt";
  ofstream file ( filename.c_str() );
  if ( file )
  {
    file<< name <<endl;
  }
}
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

ops sorry...is it C++

thanks for the help

koh
Junior Poster in Training
73 posts since Jul 2004
Reputation Points: 11
Solved Threads: 0
 

In file proceesing

how can i find the renamed file....

meaning....i created 2 files named derrick.txt and alex.txt

and in the program when i input derrick, the program will look for the file derrick and open the file,

and if i enter alex, the program will look for alex.txt


thank you

koh
Junior Poster in Training
73 posts since Jul 2004
Reputation Points: 11
Solved Threads: 0
 
can i know how to save a file name based on the user's input for example: Enter your name : Derrick then i save this input to a file .txt named derrick (derrick.txt) how can i do it? i only know how to use the rename() and delete() function...in the file processing thank you for your kindness
// in c

#include <stdio.h>

int main()
{
    char name[10];
    FILE *filePtr;

    if( ( filePtr = fopen(" myFile.txt ", "w")) == NULL)
    printf("No file found\n");
    else
    {
        printf("Enter name: ");
        scanf("%s",name);
        
        while(!feof(stdin))
        {
             fprintf(filePtr,"%s\n",name);
             scanf("%s",name);
        }
    fclose(filePtr);
    }
    
    getchar();
    
    return 0;
}
DoEds
Junior Poster in Training
63 posts since Jun 2009
Reputation Points: 6
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You