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?
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?
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
1) The simplest way would be to concatenate the string entered with a previously saved directory string:
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.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; }
My best code is written with the delete key.
One of simpliest way is make delays on nanoseconds... ( *nix )
Code:... usleep ( n ); ...
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!