Thread: Opening File

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    23

    Opening File

    My assignment is to have the user input a filename and then change that filename to all capital letters. I have no clue how to do this. Any help would be greatly appreciated. I will post the code I have so far but I have no idea where to go from here


    PHP Code:
    #include <stdio.h>

    int main()

    {
    FILE *infile;
    char filename[13];

    printf("\nEnter a file name: ");
    gets(filename);

    infile fopen(filename,"w"); 

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    gets is bad news.

    So first fix that. Then it's simply a matter of turning filename to uppercase using toUpper. Then you want to open filename in read mode, and FILENAME in write mode. Then you copy the file over.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    75
    Try this:

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #define MAX 13
    
    int main()
    
    {
       int  i;
       char old[MAX], new[MAX];
    
       printf("\nEnter a file name: ");
       fgets(old, MAX, stdin);
    
       for (i=0; old[i]!='\n'&&old[i]!='\0'; i++)
          new[i]=toupper(old[i]);
       old[i]=new[i]='\0';
    
       /*Attempt to rename*/
       if (rename( old, new )==0)
          printf( "File '%s' renamed to '%s'\n", old, new );
       else
          printf( "Failed to rename '%s'\n", old );
    
       return 0;
    }

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    23

    Gets and getting it to work

    Is fgets an appropriate substitute? My textbook constantly uses gets but everyone on this website seems to despise it.

    Here is my new rough draft. It won't compile due to the toupper line so not sure where I am going wrong on that one. Thanks for your help.

    Kevin

    Code:
    #include <stdio.h>
    #include <ctype.h>
    int main()
    
    {
    FILE *infile;
    char filename[13];
    char FILENAME[13];
    
    printf("\nEnter a file name: ");
    gets(filename);
    infile = fopen(filename,"w");
      
    toupper filename = FILENAME;
    
    printf("\nFilename is now %s",FILENAME);
    
    return 0;
    }

  5. #5
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Read the link. gets is bad news. fgets is a good substitute. And toUpper is function that takes a character and returns anoter....I don't really know what you're doing...

    After that, you should google "man rename". While you're at it, google "man toUpper".
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    toupper() takes a character and gives the character back, in uppercase. So you loop through the filename, changing each character to uppercase. I hope you've learned loops and function calls by now...
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > My textbook constantly uses gets but everyone on this website seems to despise it.
    Take it back to the shop for a refund
    Set fire to it
    Use it to prop up a wobbly table.
    Do anything except continue to learn C from it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    Quote Originally Posted by Salem
    > My textbook constantly uses gets but everyone on this website seems to despise it.
    Take it back to the shop for a refund
    Set fire to it
    Use it to prop up a wobbly table.
    Do anything except continue to learn C from it.
    Make a paper juice out of it and give it to a drunk
    Make the pages your scrap papers
    Play frisbee with your dog using that book


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM