Thread: File opening

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    TX
    Posts
    19

    Unhappy File opening

    I have the task of opening a file with a c program and having all the letters be capatilized in the opened file. First issue is that when I attempt to open the file I don't actually see the file anywhere, how can I fix that? Second is where do I start for capitalizing all the letters contained in the opened file? Any help would be appreciated.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    int main ()
    {
    FILE *inFile;
    char fileName[13];
    
    printf("\nEnter a file name:");
    gets(fileName);
    
    inFile = fopen(fileName, "r");
    
    if (inFile == NULL)
       {
       printf("\n The file cannot be opened.");
       printf("\nPlease check that the file currently exsists.");
       exit(1);
       }
    printf("\nThe file has been successfully opened for reading:");
    
    system ("PAUSE");
    return 0;
    }
    my code that should allow me to read a file.

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    First issue is that when I attempt to open the file I don't actually see the file anywhere, how can I fix that
    what do u mean the file is cant be seen any where. U cant see the file whicch u have opened by the fopen. U just know that it has opened or not through the return value of the fopen. which u have done it right.

    And this bit
    Code:
    gets(fileName);
    change that to fgets, see FAQ

    Code:
    system ("PAUSE");
    use getchar. system function takes too much processing time which need trap the OS for excution.

    and to read file contents use fgets fucntion to a read a line from the file

    ssharish2005
    Last edited by ssharish2005; 12-27-2006 at 05:39 PM.

  3. #3
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    fopen only opens for reading. It doesn't actually open the file and display it. AKA, opening a .doc file won't boot up Word to allow you to see it correctly.
    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

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    c program and having all the letters be capatilized in the opened file
    here is a simple solution for this.

    1. open the file in read mode
    2. open another file called upper.txt in append mode
    3. read a single char from the source file
    4. use toupper function to convert to upper char
    5. write the return value of toupper function to the upper.txt (ctype.h)
    6. repeat step 3 until u reach EOF file (use fgetc)
    7. Close both the file

    ssharish2005

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