I want to create a function that, given a string filename, creates the file called filename.PID inside the ./pid directory.
#define DEBUG 1
//PRINT_DEBUG just print the string in stderr
int CreatePidFile(char *filename){
if(DEBUG){
DEBUG_PRINT("CreatePidFile: start\n");
}
char *path = "./pid/";
char *post = ".PID";
FILE *pidfile;
char *pathfilename;
int N=strlen(path)+strlen(filename)+strlen(post)+1;
if((pathfilename=(char *)malloc(N*sizeof(char)))==NULL){
return -3;
}
strcpy(pathfilename, path);
strcat(pathfilename, filename);
strcat(pathfilename, post);
pathfilename[N-1]='\0'; //just to be sure that it has the final string char
if((pidfile = fopen(pathfilename, "w"))==NULL){
if(DEBUG){
DEBUG_PRINT("CreatePidFile: impossible to create il file\n");
}
free(pathfilename);
return -1;
}
int pid=getpid();
if((fwrite((void *)&pid, sizeof(int), 1, pidfile))==0){
if(DEBUG){
DEBUG_PRINT("CreatePidFile: impossible to write pid in pidfile\n");
}
fclose(pidfile);
free(pathfilename);
return -2;
}
fclose(pidfile);
free(pathfilename);
if(DEBUG){
DEBUG_PRINT("CreatePidFile: end\n");
}
return 0;
}
The main I use is:
int main(){
printf("create pid: start\n");
char *filepid = "test_pid_file";
if((CreatePidFile(filepid))!=0){
printf("file not created\n");
}
else{
printf("test_utility: file is created\n");
}
return 0;
}
At the end of the program, the file is created but is a binary file.
I want a text file.
Well, you do a binary write of the pid:
fwrite((void *)&pid, sizeof(int), 1, pidfile)
If you want text, just use fprintf:
fprintf(pidfile, "%d", (int)pid);
Related
I could use some help passing this file pointer to a thread to have the thread write to an output file. Currently the file is not being written to at all and I am not getting any errors and working.
void *ThreadTwo(void *param){
FILE *outputFile = param;
// Condition for even
if (r == 0) {
fprintf(outputFile, "%d\n", globalValue);
fprintf(outputFile, "%d\n", globalValue);
printf("twice becase it is even\n");
}
else {
fprintf(outputFile, "%d\n", globalValue);
printf("Once because it is odd\n");
}
}
pthread_exit(NULL);
}
int main() {
FILE *outputFile;
outputFile = fopen("hw3.out", "w");
pthread_create(&th[1], NULL, ThreadTwo, &outputFile);
fclose(outputFile);
return 0;
}
im writing and reading from shared memory a string.
Here it´s my code:
This is the writer (i have ommited unrelated code)
int main() {
char message[MAX_BUF];
key_t key;
int sharedMemoryId;
int semaphoreId;
char *vc1;
char *data;
pid_t p3;
struct sembuf operations[1];
printf("start p2\n");
saveMesageInBuffer(message); //This reads message from pipe and saves into message variable
if(message==NULL){
return -1;
}
key = getKeyForFile();
if(key != -1){
sharedMemoryId = createSharedMemoryId(key);
if(sharedMemoryId!=-1){
vc1 = shareContentInMemoryId(sharedMemoryId);
}
switch(p3 = fork()){
case -1:
printf("Error");
break;
case 0:
printf("run\n");
execl("./Ej3", "Ej3", NULL);
break;
default:
sleep(SECONDS);
writeMessageInSharedVariable(vc1, message);
pause();
break;
}
} else {
printf("Error getting key for file: %s\n", strerror(errno));
}
return 0;
}
void writeMessageInSharedVariable(char *dest, char *message){
printf("El proceso P2 (PID=%d, Ej2) transmite un mensaje al proceso P3 a traves de una variable en memoria compartida\n", getpid());
strncpy(dest, message, MAX_BUF);
}
int createSharedMemoryId(key_t key){
return shmget(key, MAX_BUF, IPC_CREAT | 0600);
}
char* shareContentInMemoryId(int memoryId){
return shmat(memoryId, (void *)0, 0);
}
key_t getKeyForFile(){
char filePath[1024];
if (getcwd(filePath, sizeof(filePath)) != NULL){
strcat(filePath, "/");
strcat(filePath, FIFO_FILE_NAME);
return ftok(filePath, 0777);
} else {
return (key_t) -1;
}
}
`
This is the reader (compiled as Ej3 and launched via fork and exec from writer)
int main() {
key_t key;
char message[MAX_BUF];
int sharedMemoryId;
char* vc1;
printf("el 33 \n");
key = getKeyForFile();
if(key != -1){
sharedMemoryId = createSharedMemoryId(key, sizeof(message));
sleep(3);
printf("continua\n");
vc1 = (char*)shmat(sharedMemoryId, (void *)0, 0);
if (vc1 == (char *)(-1)) {
perror("shmat");
exit(1);
}
printf("Readed %s\n", vc1);
} else {
printf("Error getting key for file: %s\n", strerror(errno));
}
}
I´m writting test in message and this is the result while i read it.
Readed test�[]���w�
strncpy(dest, message, MAX_BUF);
strncpy doesn't null-terminate the buffer.
You must null-terminate it by yourself.
strncpy(dest, message, MAX_BUF);
dest[MAX_BUF-1] = '\0'; /* <- like this */
printf(%s takes C-string which is a char array with a null character at the end. If you don't null-terminate the string, printf will not know when to stop, thus the garbage output.
I need to open a file located on Desktop(Linux). If i write the location as a string inside the fopen() function it works, but if i pass it as a variable, it doesn't work. Here is my code :
fp = fopen(readPathToFile, "r");
if (!fp){
printf("Failed to open text file\n");
exit(1);
}
else{
fscanf(fp,"%s",line);
printf("File read: %s",line);
}
If i write it like this, it shows me the content of file :
fp = fopen("home/user/Desktop/test.txt", "r");
if (!fp){
printf("Failed to open text file\n");
exit(1);
}
else{
fscanf(fp,"%s",line);
printf("File read: %s",line);
}
The child process opens the file. Here is my full code
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#define READ 0
#define WRITE 1
int main ()
{
pid_t pid;
int mypipefd[2];
id_t child_pid;
char line[100];
char *pathToFile[100];
FILE *fp;
char buff[255];
/* create the pipe */
if (pipe(mypipefd) == -1) {
fprintf(stderr,"Pipe failed");
return 1;
}
child_pid = fork () ;
if (child_pid > 0) {
printf("Introduceti locatia catre fisier:");
fgets(pathToFile, 100, stdin);
close(mypipefd[READ]);
write(mypipefd[WRITE], &pathToFile, sizeof(pathToFile));
close(mypipefd[WRITE]);
printf("parent: write value : %s",pathToFile);
}
else if (child_pid < 0) {
fprintf(stderr, "Fork failed");
return 1;
}
else{
char *readPathToFile[100];
close(mypipefd[WRITE]);
read(mypipefd[READ], &readPathToFile, sizeof(readPathToFile));
close(mypipefd[READ]);
printf("child: read value : %s",readPathToFile);
fp = fopen(readPathToFile, "r");
if (!fp)
{
printf("Failed to open text file\n");
exit(1);
}
else{
fscanf(fp,"%s",line);
printf("File read: %s",line);
}
}
return 0;
}
Your compiler did not warn you about the type mismatch in
char *pathToFile[100];
fgets(pathToFile, 100, stdin);
(array of 100 pointers-to-char versus array of 100 chars)? Did you turn warnings off?
Also note that fgets retains the newline. Your file name probably does not end with a newline. You should replace it with a NUL (zero) byte.
Typically you don't need a debugger to track these down. A little bit of printf debugging can do wonders. :-)
Okay, so this is the root of your problem:
char *pathToFile[100];
This declares pathToFile as a 100-element array of pointers to char, not a 100-element array of char. The first thing you need to do is change that declaration to
char pathToFile[100];
Secondly, fgets will save the trailing newline from your input to the target buffer if there's room, so you'll need to remove that newline from the input:
char *newline = strchr( pathToFile, '\n' );
if ( newline )
*newline = 0;
If I write
char ar[100];
strcpy(ar, "I am in child\n");
write(fd, ar, strlen(ar))
It will copy "I am in child" to file located in fd using open function.
But how to write
printf("you got %d points\n",dice);
to another file in C? dice is an integer.
You should use fprintf().
#include <stdio.h>
int main(void) {
int dice = 0;
FILE* fp = fopen("your_file_name.txt", "w");
if (fp == NULL) {
perror("fopen");
return 1;
}
fprintf(fp, "you got %d points\n", dice);
fclose(fp);
return 0;
}
Code that I am running now:
int main(int argc, char *argv[])
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind = INVALID_HANDLE_VALUE;
DWORD dwError;
LPSTR DirSpec;
size_t length_of_arg;
int i,j;
char cd[256],schar[500];
FILE *fp;
DirSpec = (LPSTR) malloc (BUFSIZE);
// Check for command-line parameter; otherwise, print usage.
if(argc != 2)
{
printf("Usage: Test <dir>\n");
return 2;
}
// Check that the input is not larger than allowed.
//scanf("%s",argv[1]);
StringCbLength(argv[1], BUFSIZE, &length_of_arg);
if (length_of_arg > (BUFSIZE - 2))
{
printf("Input directory is too large.\n");
return 3;
}
printf ("Target directory is %s.\n", argv[1]);
StringCbCopyN (DirSpec, BUFSIZE, argv[1], length_of_arg+1);
StringCbCatN (DirSpec, BUFSIZE, "\\namefile.b11", 18);
hFind = FindFirstFile(DirSpec, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
printf ("Invalid file handle. Error is %u.\n", GetLastError());
return (-1);
}
else
{
printf ("First file name is %s.\n", FindFileData.cFileName);
fp=fopen(DirSpec,"rb");
for(i=0;i< 8;i++)
{
schar[i]= fgetc(fp);//get each character from file
}
if ( i > 7 )
{
cd[i]=schar[6]*65336+schar[5]*256+schar[4];
printf("%d",cd[i]);
}
// List all the other files in the directory.
while (FindNextFile(hFind, &FindFileData) != 0)
{
printf ("Next file name is %s.\n", FindFileData.cFileName);
}
dwError = GetLastError();
FindClose(hFind);
if (dwError != ERROR_NO_MORE_FILES)
{
printf ("FindNextFile error. Error is %u.\n", dwError);
return (-1);
}
}
free(DirSpec);
getchar();
return (0);
}
This is working fine. If I concatinate the file name directly by using StringCbCatN().
But for every file I need to change the file name.which I don't want. Is it possible to print the file with file extension?