473,383 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

fread problems

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
Dec 3 '07 #1
1 2327
gpraghuram
1,275 Expert 1GB
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

Expand|Select|Wrap|Line Numbers
  1. int main() {
  2. //....your code
  3.         FILE *f = NULL;
  4.         int ret = system(COMMAND);
  5.        if(ret == 0)
  6. //....your code
  7.        f = fopen("xyz", "rw");
  8.        //You dont need to do these steps...
  9.         //fseek(f, 0, SEEK_END);
  10.         //int size = ftell(f);
  11.         //rewind(f);
  12.         //unsigned int pid[10];
  13. //      char *pid = (char*)malloc(size+1);
  14.          ///start reding the info from file
  15.         int res = fread(pid, sizeof(unsigned int), 10, f);
  16.  
Raghuram
Dec 3 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
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,...
22
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;
4
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...
9
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; .. ..
4
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)
5
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";
2
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...
1
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....
3
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...
4
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...
1
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...
0
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...
0
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...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.