Thread: Data - Writing and Writing C in C++

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    69

    Data - Writing and Writing C in C++

    Hey,

    I actually try to learn about Unix-Network - Development, but I am pretty much a Newbie. (Not studying CS, yet...)
    I am learning C++ just since 1 Month and therefore I do not want to go into C yet. (Would be confusing...)
    Unfortunately, the book uses C which leads me to the first question:

    I know you write this completely different in C++, but will the C++ Community kill me if I wrote code like this:

    Code:
    //ignore the german sentences, they are just stating what just happend wrongly
    #include <errno.h>
    #include <fcntl.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/stat.h>
    #include <stdio.h>
    #include <unistd.h>
    
    
    int main(int argc, char *argv[]){
        int fd;
        mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP |S_IROTH;
        char filename[] = "read.data";
        char text[] = "0123\n""4567\n""89ab\n""cdef\n""ghij\n""klmn\n""opqr\n""stuv\n""wxyz\n";
        ssize_t bw;
        
        fd = open(filename,O_WRONLY | O_CREAT | O_TRUNC, mode);
        if(fd < 0){
            printf( "Kann die Datei '%s' nicht öffnen: %s\n",filename,strerror(errno) );
            exit(EXIT_FAILURE);
        }
        bw = write(fd,text,strlen(text));
        if(bw != strlen(text)){
            printf("Konnte nur %zd Bytes schreiben. \n", bw);
            exit(EXIT_FAILURE);
        }
        if(close( fd ) != 0){
            printf("Konnte die Datei '%s' nicht schließen: %s\n", filename, strerror(errno));
            exit(EXIT_FAILURE);
        }
        printf("Done\n");
        return(EXIT_SUCCESS);
    }
    The second question is where read.data is created, I cannot find it in the filesystem, which leads me to the conclusion something went wrong. (Does not work with main.c either || I am using Mac)

    Kind regards,
    Niclas

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I normally agree that trying to learn C and C++ at the same time would be confusing... but from what I see you're doing it anyway. Just learn C too.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    69
    Quote Originally Posted by laserlight View Post
    I normally agree that trying to learn C and C++ at the same time would be confusing... but from what I see you're doing it anyway. Just learn C too.
    Ok, thank you. And where the file is saved?

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    The file will be created in the current directory, which should be the directory you ran the executable from, as long as you have write access to that directory.

    What does "Does not work with main.c either" mean?

    BTW, the C++ community (and the modern C community) would kill you for defining fd and bw a mile away from where you initialize them.

    EDIT: Also, the format spec "%zd" is for size_t, not ssize_t. So it's probably best to print ssize_t like this:
    Code:
            printf("Konnte nur %ld Bytes schreiben. \n", (long)bw);
    Last edited by john.c; 02-08-2019 at 02:49 PM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    69
    Hey,

    the Code is actually copied from the book, which latest version was published in 2006... (still has good reviews)
    The code might actually be older (1999) because the author is writing in old German grammar ...
    I admit being confused with all this size_t and tsize_t and long long and long long int... (most lightly because I know Java ...) I actually haven't a clue how to deal with all this...

    If I copy the Programm to another folder it works...

    $ cd ..
    $sudo /Programm
    $ cat test.data

    But in XCode, no file is created...
    Why?
    Last edited by SuchtyTV; 02-08-2019 at 02:58 PM.

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Presumably you don't have write access to whatever directory XCode is starting in. I notice that you are running it with sudo from the terminal. That shouldn't be necessary if you are running it from one of your own directories.

    Try adding this to the beginning of the program and running it in XCode. It should tell you the directory it is in.
    Code:
        char buf[1000];
        if (getcwd(buf, sizeof buf) == NULL) {
            perror("getcwd");
            exit(EXIT_FAILURE);
        }
        printf("dir: %s\n", buf);
    A little inaccuracy saves tons of explanation. - H.H. Munro

  7. #7
    Registered User
    Join Date
    Feb 2019
    Posts
    69
    1. What exactly is getcwd and peerror() doing?
    2. My Output is: /Users/niclas/Library/Developer/Xcode/DerivedData/Program-gjhkltdqgrdwcyfwfjnhetppjpgl/Build/Products/Debug
    (Which is defintely stupid, my Terminal could not find this file by:

    $ cd ..
    $ find /* test.data

    But it is there! I actually do not know why xCode is creating this in my Lib, where my workspace is in my Docs...)

    Thank you!!!

  8. #8
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Look up functions that you don't know (you'll be doing a lot of that!).

    And you aren't using "find" correctly. It should be find / -name read.data
    A little inaccuracy saves tons of explanation. - H.H. Munro

  9. #9
    Registered User
    Join Date
    Feb 2019
    Posts
    69
    Works fine. Thank you.
    Mostly lightly this won't be the last question, but lightly for the day.

    Have a good night!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. writing good code is like writing an artistic expression
    By renzokuken01 in forum C Programming
    Replies: 5
    Last Post: 02-03-2011, 08:48 PM
  2. Writing raw USB data
    By Poincare in forum Linux Programming
    Replies: 4
    Last Post: 05-22-2010, 08:54 PM
  3. Writing to a Data File
    By joxerjen in forum C++ Programming
    Replies: 6
    Last Post: 10-15-2005, 03:39 AM
  4. i need your help about writing data to file
    By john_tran in forum C++ Programming
    Replies: 3
    Last Post: 10-15-2004, 10:42 AM
  5. reading and writing data
    By chrismiceli in forum C Programming
    Replies: 1
    Last Post: 09-14-2003, 11:48 AM

Tags for this Thread