Help with C in linux Ubuntu 12.04


 
Thread Tools Search this Thread
Top Forums Programming Help with C in linux Ubuntu 12.04
# 8  
Old 06-29-2012
They are simply numbers, and are always the same. STDOUT_FILENO is just to make it obvious that it is indeed a file.

When you start a process in a terminal, it gets three pre-opened files: standard input, standard output, and standard error. If you don't redirect them, they all talk directly to the terminal(stdin reads, stdout/stderr both write). However, the terminal itself can redirect them to wherever it pleases before the program is run ( which is how echo whatever > /dev/ttyUSB1 works -- the shell redirects the standard output of echo into /dev/ttyUSB1 )

Code:
$ cat > file_nums.c <<EOF
#include <stdio.h>
#include <unistd.h>

int main(void)
{
        printf("STDIN_FILENO=%d\n", STDIN_FILENO);
        printf("STDOUT_FILENO=%d\n", STDOUT_FILENO);
        printf("STDERR_FILENO=%d\n", STDERR_FILENO);
}

EOF

$ gcc file_nums.c -o file_nums

$ ./file_nums

STDIN_FILENO=0
STDOUT_FILENO=1
STDERR_FILENO=2

$

Opening another file is easy however:
Code:
int fd=open("filename", O_RDWR|O_CREAT|O_TRUNC, 0660);
write(fd, ...);

See man 2 open for further information.

Last edited by Corona688; 06-29-2012 at 06:51 PM..
# 9  
Old 06-29-2012
I have this code:

Code:
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>


int main(void)
{
    //FILE*out;        
    ssize_t ret;
        char buf[512];
        int fd=open("/dev/ttyUSB1", O_RDWR);
    int out = open("/home/hectromasencio/Desktop/prueba.txt", O_RDWR|O_CREAT|O_TRUNC);
        if(fd < 0) return(1);
//commands to start streaming data
        write(fd, "#o1\n", 4);
    sleep(3);
//command to stop streaming data
    write(fd, "#o0\n",4);

        while( (ret=read(fd, buf, 512)) > 0)
                write(out, buf, ret);
        //fwrite(buf,4,sizeof(buf),out);
//STDOUT_FILENO "/home/hectormasencio/Desktop/prueba.txt"
        close(fd);
        return(0);
}


what I'm trying to do is, I have an IMU I already program it using Arduino IDE, now I want to send a command to start streaming and another command to stop streaming data then I want to save that data into a text file. I run this code and it compile but it doesn't do anything I have no response from the IMU nor the terminal telling me something is wrong.

Moderator's Comments:
Mod Comment Code tags for code, please.

Last edited by Corona688; 06-30-2012 at 02:40 AM..
# 10  
Old 06-30-2012
How is it supposed to tell when the data ends?

Try making a smaller program, and building onto it. See if you can get your arudino sending data at all first.
# 11  
Old 06-30-2012
I was able to read the data. the problem that I have is when I want to save that data into a text file. When I execute the program it gave me this error "segmentation fault (core dumped)"
# 12  
Old 07-03-2012
I can't possibly tell why it's crashing from here unless you actually post your program.
Login or Register to Ask a Question

Previous Thread | Next Thread

4 More Discussions You Might Find Interesting

1. Ubuntu

Application clustering in Linux Ubuntu

Dear, Please help me to configure application clustering in linux ubuntu. Application is running apache server. Please help Jewel ---------- Post updated at 01:07 PM ---------- Previous update was at 12:48 PM ---------- linuxvirtualserver dot org in this link i go there's three... (2 Replies)
Discussion started by: Jewel100
2 Replies

2. Ubuntu

Modifying PATH (LINUX, Ubuntu)

Hello, I'm a newbi to Unix and the last few weeks I have been trying to learn Unix through a book called Unix in 24 hours. I have tried advanced shell programming (that's what the chapter is called) today and what the excersise was all about was to create mylocate - a version of locate that is... (1 Reply)
Discussion started by: Tolmac
1 Replies

3. UNIX for Dummies Questions & Answers

How do I mount a new disk on ubuntu linux?

Hello, I am fairly new to ubuntu and have been learning linux using this distro. I am using Ubuntu 11.04 server. Recently I added a new HD to the desktop.. however I know I need to mount it, My question is how would I mount this disk? so I can use it as a drive? Thanks here are my... (5 Replies)
Discussion started by: NelsonC
5 Replies

4. Ubuntu

XP and Linux (Ubuntu) on same disk, Can I install Ubuntu on not-yet partitioned portion of disk?

My PC (Esprimo, 3 yeas old) has one hard drive having 2 partitions C: (80 GB NTFS, XP) and D: (120 GB NTFS, empty) and and a 200 MB area that yet is not-partitioned. I would like to try Ubuntu and to install Ubuntu on the not-partitioned area . The idea is to have the possibility to run... (7 Replies)
Discussion started by: C.Weidemann
7 Replies
Login or Register to Ask a Question