|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
a few questions:
1) i'm writing a program that requires the opening of a file from a specific directory. i know the syntax to create a pointer to a file, however i am unclear as to how i could set the pointer to open a file like this: /home/username/./somedir/filename -- also how do i write the file back to that directory once i've made my editions to it? 2) how do i call system commands from a C program. for example how would i make the app call xmms for example /usr/bin/xmms ? 3) what is the syntax for setting up your main() to receive parameters from the command line? feedback on any of the above is appreciated. thanks! -j |
|
#2
|
||||
|
||||
|
1. Specify the complete pathname in the fopen function
Code:
#include <stdio.h>
....
....
FILE *in, *out;
/* To open a file in read mode */
in = fopen("/home/username/./somedir/filename", "r");
/* To open a file in write mode */
out = fopen("/home/username/./somedir/filename2", "w");
/* To open a file in append mode */
out = fopen("/home/username/./somedir/filename2", "a");
/* To open a file in read/write mode */
in = fopen("/home/username/./somedir/filename", "r+");
2. Use the system() call Code:
#include <stdlib.h>
....
....
system("/usr/bin/xmms");
3. Declare the function as: int main(int argc, char *argv[]) or int main(int argc, char **argv) argc will give you the number of arguments that are passed to the program and the argv array holds the actual arguments. argc will always be >=1 and argv[0] always holds the name of the program itself. Code:
#include <stdio.h>
int main(int argc, char *argv[]) {
int x;
for (x = 0; x < argc; x++)
printf("Argument #%d is %s\n", x, argv[x]);
return 0;
}
|
|
#3
|
|||
|
|||
|
great, thanks so much
![]() |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > opening a C file from a specific directory |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|