searching for/creating a file in a user-specified folder

This is a discussion on searching for/creating a file in a user-specified folder within the C Programming forums, part of the General Programming Boards category; hi, i am writing a simple program whereby the user is asked to key in the full path of a ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    45

    searching for/creating a file in a user-specified folder

    hi,

    i am writing a simple program whereby the user is asked to key in the full path of a folder, and then the program is to search for a file with a particular name, open it for processing, and then to create an output file with a fixed name in that same folder. for instance, the program may look for a file named 'image' and output something like 'processed_image'.

    are there some straightforward codes to do that? thanks!

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Sure, what OS?

    If Windows, search MSDN (or browse).
    If Linux, search the manual.

    The site FAQ may also have some info.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by bored_guy View Post
    hi,

    i am writing a simple program whereby the user is asked to key in the full path of a folder, and then the program is to search for a file with a particular name, open it for processing, and then to create an output file with a fixed name in that same folder. for instance, the program may look for a file named 'image' and output something like 'processed_image'.

    are there some straightforward codes to do that? thanks!
    There are various file handling functions in C. Usually including something akin to <dir.h> that has these functions.

    I haven't used them, so I can't advise you on them. Here's a "roll your own" approach:

    Code:
    #include <stdio.h>
    
    
    int main() {
      FILE *fp;
      char fullFileName[100];
      int i;   
    
      fgets(fullFileName, sizeof(fullFileName), stdin);
      /* e.g.: C:\TC\file_get.txt<enter>
      i = 99;
      while(i > -1) {   //must strip off the newline on the end
        if(fullFileName[i] == '\n')
          fullFileName[i] = '\0';  //and properly adjust the string
        --i;
      }
    
      if((fp = fopen(fullFileName, "rt"))==NULL) {
        perror("Error opening fullFileName");
        return 1;
      }
      while((fgets(fullFileName, sizeof(fullFileName), fp)) != NULL)
        printf("\n\n\n %s", fullFileName);
    
      i = getchar(); ++i;
      return 0;
    }
    You know you're getting off very easy here. Next time, be prepared to show some code, partner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  2. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 04:49 PM
  3. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 03:53 PM
  4. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 08:01 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21