Answer» I used command "echo 171 > /sys/class/gpio/export" to create a folder "/sys/class/gpio/gpio171/" and some files inside it. For some reason, I must do the same thing in C language. I used fd = fopen ("/sys/class/gpio/export", "wb"); fwrite ("171\n", 1, 4, fd) fclose (fd)
to try to do the same thing done by 'echo' command. But it doesn't work. That's the statements above didn't achieve the result as 'echo' command did.
What's wrong with it?Well, for starters, each is a separate language.
There are many forums that provide examples. http://en.allexperts.com/q/C-1587/use-fwrite.htm
Did you try to use an example from a tutorial? Like hiss: Code: [SELECT]#include<stdio.h> void main() { FILE *fp = NULL; short x[10] = {1,2,3,4,5,6,5000,6,-10,11}; short result[10]; fp=fopen("c:\\temp.bin", "wb"); if(fp != NULL) { fwrite(x, 2 /*sizeof(short)*/, 10 /*20/2*/, fp); REWIND(fp); fread(result, 2 /*sizeof(short)*/, 10 /*20/2*/, fp); } else exit(0); That is not a FULL program, just a fragment that shows use of fwrite and fread. Does that help any?I did it but didn't work. So, why does driver for /sys/class/gpio/export have different behavior to "echo command" and "c functions"?As I said, different languages. But ignore the rest of my post. You just don't get it. An echo is a part pf the bash thing. Here is a bash tutorial. http://www.hypexr.org/bash_tutorial.php
C is C. It is not bash. Nor is one a subset of the other. Trying to learn C while you are playing with bash is like learning Japanese and working on Spanish.
Before you try binary files in C, you need to understand the basic nI/O methods.
C File I/O and Binary File I/O
With simple file I/O, you must first open a file for write, then write and then close the file. Next open the file for read and read it, then close. That is s basic file I/O is most languages.
Stop comparing bash to C - unless you think hot dogs grow on trees.
|