directory file openong

This is a discussion on directory file openong within the C Programming forums, part of the General Programming Boards category; hi! 1)my program ask my to open a file which is in a directory named DIR.how can i do this(i ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    20

    Smile directory file openong

    hi!
    1)my program ask my to open a file which is in a directory named DIR.how can i do this(i want to enter only the file name,without the complete path)?
    2)how can i display time and date who change every second, and make delays lower than 1ms?

  2. #2
    Welcome to the real world
    Join Date
    Feb 2004
    Posts
    50
    Use a FILE type.


    Code:
    #include <stdio.h>
    
    int main (int argc, char *argv[])
    {
       FILE *fp;
    
       if ( (fp = open("filename", r)) != NULL)
       {
           printf("error opening file.\n");
       }
    
       /* now do things with the file */

    There is a good article that deals with "working with files in *nix" on this website. Look at the tutorials section. The tutorials mostly cover both Win/*nix

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    1) The simplest way would be to concatenate the string entered with a previously saved directory string:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    FILE *
    open_file_in(
      const char *path,
      const char *filename,
      const char *mode
      )
    {
      char buf[BUFSIZ];
    
      strcpy(buf, path);
      strcat(buf, filename);
    
      return fopen(buf, mode);
    }
    
    int
    main(void)
    {
      FILE *in = open_file_in("C:\\", "test.txt", "r");
      char ch;
    
      if (!in) {
        perror(NULL);
      }
      while ((ch = getc(in)) != EOF) {
        putc(ch, stdout);
      }
      fclose(in);
    
      return 0;
    }
    2) Why do you need delays less than 1ms when the time only changes each second? Displaying the date and time is simple if you do a search on this forum. In fact, I believe the FAQ board has one potential answer to that question. High resolution delays are harder, and the solution depends on your operating system.
    My best code is written with the delete key.

  4. #4
    SleepWalker tjohnsson's Avatar
    Join Date
    Apr 2004
    Posts
    70
    One of simpliest way is make delays on nanoseconds... ( *nix )
    Code:
       ...
       usleep ( n ); 
       ...

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    20

    Cool founded solution

    people, look what i asked for:

    #include <stdio.h>
    #include <string.h>

    void main(void){

    FILE *fp;
    char buf[40],filename[13];
    strcpy(buf,"DIRNAME/");
    clrscr();
    printf("enter the file name:\n");
    gets(filename);
    strcat(buf,filename);

    fp=fopen(buf,"rb");
    if(fp==NULL){
    printf(" file can't be opened");
    exit(1);
    }
    ......
    main programme
    ......
    fclose(fp);
    exit(0);
    }
    it really works!
    was that so complicated?
    prelude, thanks for the idea!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 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. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

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