We are working on a homework on CELL programming for college and their feedback response to our questions is kinda slow, thought i can get some faster answers here.
I have a PPU side code which tries to open a file passed down through char* argv[], however this doesn't work it cannot make the assignment of the pointer, i get a NULL.
Now my first idea was that the file isn't in the correct directory and i copied in every possible and logical place, my second idea is that maybe the PPU wants this pointer in its LS area, but i can't deduce if that's the bug or not. So...
My question is what am i doing wrong?
I am working with a Fedora 7 SDK Cell, with Eclipse as an IDE. Maybe my argument setup is wrong tho he gets the name of the file correctly.
Code on request:
images_t *read_bin_data(char *name)
{
FILE *file;
images_t *img;
uint32_t *buffer;
uint8_t buf;
unsigned long fileLen;
unsigned long i;
//Open file
file = (FILE*)malloc(sizeof(FILE));
file = fopen(name, "rb");
printf("[Debug]Opening file %s\n",name);
if (!file)
{
fprintf(stderr, "Unable to open file %s", name);
return NULL;
}
//.......
}
Main launch:
int main(int argc,char* argv[]) {
int i,img_width;
int modif_this[4] __attribute__ ((aligned(16))) = {1,2,3,4};
images_t *faces, *nonfaces;
spe_context_ptr_t ctxs[SPU_THREADS];
pthread_t threads[SPU_THREADS];
thread_arg_t arg[SPU_THREADS];
//intializare img_width
img_width = atoi(argv[1]);
printf("[Debug]Img size is %i\n",img_width);
faces = read_bin_data(argv[3]);
//.......
}
Thanks for the help.
I got it, if anyone else had issues with it you have to enable the upload rules and upload the extra-files you desired to be used by the simulator. :)
Exactly which line is failing, and how?
You should look at errno to see what error is being returned from fopen or other calls.
Also, it should not cause this problem, but you don't need the line:
file = (FILE*)malloc(sizeof(FILE));
That memory will just be leaked...
Related
I have this code, note that it is shortened down. The problem is if the file exists
it still overwrites it. Been 30 years since I did any programming so bear with me. Thanks!
FILE *openFil(FILE *open, char namn[]);
int main(int argc, const char * argv[])
{
FILE *openFil(FILE *open, char namn[]);
FILE *anmal =NULL;
char filNamn[] = "ANMAL.DAT";
postTypAnm pAnm;
anmal = openFil(anmal, filNamn);
}
FILE *openFil(FILE *pointer, char name[])
{
if ((pointer =fopen(name, "r+b"))== NULL)
if ((pointer =fopen(name, "w+b"))== NULL)
{
/* It Enters here as well, but it should not do that or????? */
printf("error\n");
exit(0);
}
return pointer;
}
If you're using the C11 standard you can use the "x" argument to specify that if the file exists the fopen() function will fail.
For reference: http://www.cplusplus.com/reference/cstdio/fopen/
Here's a working example.
#include <errno.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
bool openFile(FILE **ptr,
const char *fileName);
int main(int argc, const char * argv[]) {
FILE *anmal = NULL;
const char *fileName = "ANMAL.DAT";
if(!openFile(&anmal, fileName)) {
exit(EXIT_FAILURE);
}
}
bool openFile(FILE **ptr,
const char *fileName)
{
*ptr = fopen(fileName, "w+bx");
if(!*ptr) {
perror("Error opening file: ");
return false;
}
return true;
}
This is using the x extension in GNU C to test whether the file exists.
As other people have pointed out, there are numerous problems in your original code.
You've redeclared the function prototype for openFil within main.
In openFil there's no point in both accepting the FILE pointer as
a parameter and overwriting it with the return value. Especially if
you're expecting to input a NULL pointer and use the function to
initialise it. Either use a pointer-to-pointer as a parameter so you
can modify the pointer within the function, or ignore it completely
and set its value with the function's return value. Not both.
You're not actually testing for whether the file exists at all. According to the manual pages for fopen (man fopen) neither of the flags you used in opening the file (r+ and w+) care whether the file exists. r+ opens for reading/writing and always positions the stream at the beginning of the file. w+ opens for reading/writing, truncating the file if it exists already. This explains why you didn't get the effect you intended.
1.
"It Enters here as well, but it should not do that or?????"
No, It shouldn´t. If both pointers are NULL the opening of the streams to ANMAL.DAT were not successful, neither in w+b nor in r+b mode. Proof if the file really exist in the directory of the executable. Else try to use the entire path from the root directory to the file.
"The problem is if the file exists it still overwrites it."
Why do you know that the file is really overwritten in a proper manner?
Since if ((pointer =fopen(name, "r+b")) == NULL) and if ((pointer = fopen(name, "w+b"))== NULL) both fail, it seems that the ANMAL.DAT does not exist where fopen() searches for it or otherwise an error occurs when trying to open it (maybe has incorrect format or is corrupted?).
Place perror(name) in the error routine to check if errno was set to indicate an error at name.
2.
In the error routine: exit(0) is not correct if an error has happened. Use exit(EXIT_FAILURE).
Side note:
You have another prototype for the function openFil-> FILE *openFil(FILE *open, char namn[]); inside of main, which is redundant.
Also the identifier of the second parameter is different in the prototype before main to the identifier at the definition of openFil, namn in comparison to name.
Trying my hand at writing some C scripts, I have some code that should save a string to a file before rebooting the system. Both work separately but trying to write to file immediately before rebooting fails...
int writeToConfFile(char* filename, char* newConf) {
FILE *fp;
int status;
fp = fopen(filename, "w");
fprintf(fp,"%s",&newConf[0]);
status = fclose(fp);
return status;
}
int main(int argc, char **argv){
char extraString[1024];
strcpy(extraString,"0");
writeToConfFile("/etc/filename", extraString);
reboot(RB_AUTOBOOT);
}
adding sleep(10) between writeToConfFile and reboot does the trick, but I would like to do it in a neater way.
edit: the os is a heavily customized legacy debian.
edit2: tried changing writeToConfFile to end like this:
fp = fopen(filename, "w");
fprintf(fp,"%s",&newConf[0]);
fflush(fp);
status = fclose(fp);
return status;
but it didn't work either
As mathieu and jamieguinan suggested, calling sync() before reboot makes sure changes are written to disk
so the code ends thus:
writeToConfFile("/etc/filename", extraString);
sync();
reboot(RB_AUTOBOOT);
Hi guys i have a problem when I try to open a file. In a function when i try to read an existing text file, after i initialized the file pointer i still get the error "cannot open the file", this is the code:
FILE * fp;
fp = NULL;
fp=fopen("results.txt","r");
if(fp==NULL){
printf("error!");
exit(1);
}
using the debugger i can see the fp initialized to NULL, as requested. In the next order i can see its value changed to '0x751d9c68'.
So now it's not NULL, but the program still prints error.
PS: I used the same code to open another file in another program (that works): as always the initial value of fp is NULL, then it's changed to '0x751d9c68' (yes, it has the same value in both programs), but this time works, because fp is in fact different from NULL.
PPS: I'm using Codelite, if that helps.
EDIT: adding a printf("%p\n", fp); prints this "751D9C68"
Atleta * leggiRisultati (char fileName [], int * dim){FILE * fp; int count, i;
Atleta temp;
fp = NULL;
fp=fopen(fileName,"r");
printf("%p\n", fp);
if(fp==NULL){
perror("Error");
}
while (fscanf (fp, "%s%s%d%d%d", temp.cod, temp.nome, &temp.tN, &temp.tB, &temp.tC)== 5)
count ++;
rewind (fp);
Atleta * atl = (Atleta*) malloc(count * sizeof(Atleta));
for (i=0; i<count; i++){
int nr = fscanf(fp, "%s%s%d%d%d",atl[i].cod, atl[i].nome, &atl[i].tN, &atl[i].tB, &atl[i].tC);
//just controlling if the reading is done properly
if (nr < 4) {
printf ("cannot read the file %s",fileName);
exit (1);
}
} fclose(fp);
return atl;
}
I then use this function in this main
int main (){ int dim; Atleta * a; int i;
a = leggiRisultati("risultati.txt", &dim);
for (i =0; i<dim;i++){
stampaRisultato(a[i]);}
return 0;
}
Where "stampaRisultato" prints a line of the file just read and "Atleta" is a struct defined as:
typedef struct {
char cod[5];
char nome[21];
int tN, tB, tC;
}Atleta;
And last, yes the text file is in the same directory as my executable, yes I have the permission to open the file, the file contains a certain number of lines with 2 strings and 3 int each.
Your code should work, I can only think of 3 things that may cause this issue.
In my experience, it's oftentimes the simplist mistakes that get you, because you're so focused on the more complex elements that some things slip your mind. I can't see the rest of your program, so forgive me if any of these answers seem patronizing. Here are the first things I would check:
1.) file permissions. Make sure you're a user with permission to access and/or change the file in question. This is a pretty easy fix on linux, but I don't know about windows.
2.) file location. Make sure your text file is in the same directory as your executable. You'll need to do this if you don't specify file location.
3.) #include statements. Sometimes even the best of us get too excited to get into the bulk of our program, and we forget to include stdio.h and/or stdlib.h. If this is the case you may run into an issue where you set the file pointer to null, and then the fopen function doesn't run, so your pointer remains null.
I am working on a quadcopter project with Beaglebone.
I need help with using pwm on Beaglebone through a C program.
I have attached the following code,
#include <stdio.h>
#include <string.h>
#include <unistd.h>
struct pwm
{
char period[100];
char duty[100];
char polarity[100];
char run[100];
}pwm1,pwm2,pwm3,pwm4;
char pwm_1[]="P9_21";
char pwm_2[]="P9_14";
char pwm_3[]="P8_13";
char pwm_4[]="P9_42";
int initialize(struct pwm &pwmi, char pwm_i[])
{
sprintf(path,"echo \"bone_pwm_%s\" >> /sys/devices/bone_capemgr.9/slots",pwm_i);
fp = popen(path,"r");
fflush(fp);
usleep(1000);
sprintf(path,"ls /sys/devices/ocp.3/pwm_test_%s.*/period",pwm_i);
fp = popen(path,"r");
while(fgets(path,100,fp)!=NULL)
strcpy(pwmi.period,path);
fflush(fp);
sprintf(path,"ls /sys/devices/ocp.3/pwm_test_%s.*/duty",pwm_i);
fp = popen(path,"r");
while(fgets(path,100,fp)!=NULL)
strcpy(pwmi.duty,path);
fflush(fp);
sprintf(path,"ls /sys/devices/ocp.3/pwm_test_%s.*/polarity",pwm_i);
fp = popen(path,"r");
while(fgets(path,100,fp)!=NULL)
strcpy(pwmi.polarity,path);
fflush(fp);
sprintf(path,"ls /sys/devices/ocp.3/pwm_test_%s.*/run",pwm_i);
fp = popen(path,"r");
while(fgets(path,100,fp)!=NULL)
strcpy(pwmi.run,path);
fflush(fp);
pclose(fp);
return 0;
printf("%s%s%s%s",pwmi.period,pwmi.duty,pwmi.polarity,pwmi.run)
}
int pwmperiod(struct pwm &pwmi, unsigned int period)
{
sprintf(path,"echo %d > %s", period, pwm.period);
fp = popen(path,"r");
usleep(1000);
pclose(fp);
return 0;
}
int main()
{
unsigned int period = 200000;
initialize(pwm1,pwm_1);
initialize(pwm2,pwm_2);
initialize(pwm3,pwm_3);
initialize(pwm4,pwm_4);
pwmperiod(pwm1,period);
return 0;
}
Now the above code works perfectly fine. But I want to use the pwmperiod() function a little differently. Instead of using popen() all the way, I want to use fopen() and fprintf() for the function pwmperiod() . Something like this,
int pwmperiod(struct pwm &pwmi, unsigned int period)
{
fp = fopen(pwmi.period,"r+");
fseek(fp,0,SEEK_SET);
fprintf(fp,"%d",period);
fclose(fp);
return 0;
}
I tried the modified code but when it attempts to write the period value, it outputs "segmentation fault".
I realized that fopen() takes a const char while pwmi.period is just char. Another problem popen() and sprint() are not compatible with const char.
So is there a way to resolve the conversion?
Also how often is popen() used in C/C++ programs?
PS:
Not an expert coder and I am not from a computer science background. I am learning progressively.
Again, the code works with popen() perfectly. But then I am comfortable with file handling in C. So I would prefer personally fopen() over popen() . Moreover I feel it would be pointless to use popen() in C. Might as well use a shell script for pwm.
Leaving aside for a moment the issue of char* vs. const char* (since a char* can anyway be passed to any function taking a const char*).
Have you checked that the return value from fopen is non-NULL?
Note that the file must exist when fopen is called with r+. If it doesn't, fopen will return NULL, generating a segfault.
Since the file is only being written, not read, consider using
fp = fopen(pwmi.period,"w");
which will create a new file if one doesn't already exist.
I would like to read an image, lets say, picture.png in C. I know I can open it in binary mode, and then read - it's pretty simple.
But I need something more: I would like to be able to read the image once, store it in my code, for example, in *.h file, as 'string of bytes', for example:
unsigned char image[] = "0x87 0x45 0x56 ... ";
and then, be able to just do:
delete physical file I read from disk,
save image into file - it will create my file once again,
EVEN if I removed image from disk (deleted physical file picture.png I read earlier) I will still be able to create an image on disk, simply by writing my image array into file using binary mode. Is that possible in pure C? If so, how can I do this?
There's even a special format for this task, called XPM and a library to manipulate these files. But remember due to its nature it's suitable only for relatively small images. But yes, it was used for years in X Window System to provide icons. Well, those old good days icons were 16x16 pixels wide and contained no more than 256 colors :)
Of course it's possible, but it's a bit unclear what you're after.
There are stand-alone programs that convert binary data to C source code, you don't need to implement that. But doing it that way of course means that the image becomes a static part of your program's executable.
If you want it to be more dynamic, like specifying the filename to your program when it's running, then the whole thing about converting to C source code becomes moot; your program is already compiled. C programs can't add to their own source at run-time.
UPDATE If all you want to do is load a file, hold it in memory and then write it back out, all in the same run of your program, that's pretty trivial.
You'd use fopen() to open the file, fseek() to go to the end, ftell() to read the size of the file. Then rewind() it to the start, malloc() a suitable buffer, fread() the file's contents into the buffer and fclose() the file. Later, fopen() a new output file, and fwrite() the buffer into that before using fclose() to close the file. Then you're done. You can do it again, as many times as you like. It can be an image, a program, a document or any other kind of file, it doesn't matter.
pic2h.c :
#include <stdio.h>
int main(int argc, char *argv[]){
if(argc != 3){
fprintf(stderr, "Usage >pic2h image.png image.h\n");
return -1;
}
FILE *fi = fopen(argv[1], "rb");
FILE *fo = fopen(argv[2], "w");
int ch, count = 0;
fprintf(fo, "extern unsigned char image[];\n");
fprintf(fo, "unsigned char image[] =");
while(EOF!=(ch=fgetc(fi))){
if(count == 0)
fprintf(fo, "\n\"");
fprintf(fo, "\\x%02X", ch);
if(++count==24){
count = 0;
fprintf(fo, "\"");
}
}
if(count){
fprintf(fo, "\"");
}
fprintf(fo, ";\n");
fclose(fo);
fclose(fi);
return 0;
}
resave.c :
#include <stdio.h>
#include "image.h"
int main(int argc, char *argv[]){
if(argc != 2){
fprintf(stderr, "Usage >resave image.png\n");
return 0;
}
size_t size = sizeof(image)-1;
FILE *fo = fopen(argv[1], "wb");
fwrite(image, size, 1, fo);
fclose(fo);
return 0;
}