Hi all,
I am writign a program to get the process no(linux) into an integer. i am not able to get the process no into integer array...this is the program.
please help me.
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#define COMMAND "pgrep BTComm > xyz"
int main() {
FILE *f = fopen("xyz", "rw");
int ret = system(COMMAND);
if(ret == 0)
printf("Command execution successful\n");
else if(ret < 0)
printf("System() Error\n");
else
printf("ret is not valid \n");
fseek(f, 0, SEEK_END);
int size = ftell(f);
rewind(f);
unsigned int pid[10];
// char *pid = (char*)malloc(size+1);
int res = fread(pid, sizeof(unsigned int), 10, f);
if(res != size)
printf("fread error\n");
printf("Process ID is: %d\n", pid[0]);
int x = kill(7487, SIGKILL);
if(x)
printf("Failed to kill process\n");
fclose(f);
}
in the place of 7487 in int x = kill(7487, SIGKILL); i want to put the processid.
if i put the char* in fread, i get the processid written to the file but not witht he integer.
please help me.
Thanks,
Kiran
1 2327
Hi all,
I am writign a program to get the process no(linux) into an integer. i am not able to get the process no into integer array...this is the program.
please help me.
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#define COMMAND "pgrep BTComm > xyz"
int main() {
FILE *f = fopen("xyz", "rw");
int ret = system(COMMAND);
if(ret == 0)
printf("Command execution successful\n");
else if(ret < 0)
printf("System() Error\n");
else
printf("ret is not valid \n");
fseek(f, 0, SEEK_END);
int size = ftell(f);
rewind(f);
unsigned int pid[10];
// char *pid = (char*)malloc(size+1);
int res = fread(pid, sizeof(unsigned int), 10, f);
if(res != size)
printf("fread error\n");
printf("Process ID is: %d\n", pid[0]);
int x = kill(7487, SIGKILL);
if(x)
printf("Failed to kill process\n");
fclose(f);
}
in the place of 7487 in int x = kill(7487, SIGKILL); i want to put the processid.
if i put the char* in fread, i get the processid written to the file but not witht he integer.
please help me.
Thanks,
Kiran
There is a porblem in the code.
First u are opening the file and calling the system command which in-turn creates the file.
So the descriptor what you have used for opening wont have any information about the file contents.
So make this change -
int main() {
-
//....your code
-
FILE *f = NULL;
-
int ret = system(COMMAND);
-
if(ret == 0)
-
//....your code
-
f = fopen("xyz", "rw");
-
//You dont need to do these steps...
-
//fseek(f, 0, SEEK_END);
-
//int size = ftell(f);
-
//rewind(f);
-
//unsigned int pid[10];
-
// char *pid = (char*)malloc(size+1);
-
///start reding the info from file
-
int res = fread(pid, sizeof(unsigned int), 10, f);
-
Raghuram
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Josh Wilson |
last post by:
The fread() is what I believe is having trouble, but I do not know
why. I know that the temp file is created and written to (and w/ the
appropriate amount of data). fread() continues to return 0,...
|
by: Rajshekhar |
last post by:
Hi ,
i am writing a simple prgm to read a .txt file then store the contents
into the array...
program as follows:
--------------------------
#include<stdio.h>
int main()
{
FILE *fp1;
|
by: bill |
last post by:
I am confused by the behavior of the following code. The function
copy() takes two FILE *'s and copies the data from one to the other.
In the main routine, I create a pipe and copy stdin to the...
|
by: Franz Jeitler |
last post by:
Hello,
I have some problems with fread.. first, let's see a part of the source
file:
FILE *fp;
char buf;
size_t nread;
..
..
|
by: janssenssimon |
last post by:
//de structure om de highscores in op de slagen
typedef struct score{
char *naam;
int veld;
int score;
struct score *volg;
}HIGH;
void toonhighscores(void)
|
by: howa |
last post by:
are there any advantage in replacing all fread() operations with
file_get_contents() ?
i.e.
file_get_contents("/usr/local/something.txt")
VS
$filename = "/usr/local/something.txt";
|
by: elisa |
last post by:
Dear all,
I have problems in writeing and reading a block of data (long array) with fread and fwrite. If I write and read an integer array, everything looks fine, but when I try long array, sth...
|
by: Marilia |
last post by:
Good evening,
(I am having a problem that is making me feel stupid. As in, I was expecting some problems (as always), but later on!)
I have a binary file which needs to be copied and processed....
|
by: juleigha27 |
last post by:
Hi,
First off, I want to apologize if this already got posted it seemed
like something happened when I tried to post it previously and it
didn't work.
I am new to file manipulation with c. I...
|
by: Highlander2nd |
last post by:
Hello there.
I'm Andrew Lucas, I'm a programmer for Half-Life. I've been working on stencil shadows lately, and I've been having problems saving mesh data for my models.
When I store mesh data, I...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
| |