Cannot write to file in C programImprove INSERT-per-second performance of SQLite?Can I ensure that, when fopen()-ing a file for “w”, the program doesn't create a file?Write lines to a text file during program execution - faster wayfprintf not writing to file, despite fclose()C program not writing to text fileWhy I cannot open another file?C - Create/Write/Read named pipeWrite program that pretends to be a TTYfclose(stdout) vs close(STDOUT_FILENO) - CReleasing a file write lock in C?

Word for being out at night during curfew

What is the best way for a skeleton to impersonate human without using magic?

How to prevent cooked noodles & dumplings from getting soggy

For the erase-remove idiom, why is the second parameter necessary which points to the end of the container?

Why in a Ethernet LAN, a packet sniffer can obtain all packets sent over the LAN?

Usefulness of complex chord names?

How do I compare the result of "1d20+x, with advantage" to "1d20+y, without advantage", assuming x < y?

What's special about a Bunsen burner?

How to Access data returned from Apex class in JS controller using Lightning web component

How can a Lich look like a human without magic?

Would an 8% reduction in drag outweigh the weight addition from this custom CFD-tested winglet?

Is taking modulus on both sides of an equation valid?

Extrude the faces of a cube symmetrically along XYZ

Create a list of all possible Boolean configurations of three constraints

Does Lawful Interception of 4G / the proposed 5G provide a back door for hackers as well?

Smallest Guaranteed hash collision cycle length

Is it a bad idea to replace pull-up resistors with hard pull-ups?

How can this pool heater gas line be disconnected?

How can I answer high-school writing prompts without sounding weird and fake?

Speculative Biology of a Haplodiploid Humanoid Species

Drawing lines to nearest point

Does the 500 feet falling cap apply per fall, or per turn?

Reaction of borax with NaOH

Who was this character from the Tomb of Annihilation adventure before they became a monster?



Cannot write to file in C program


Improve INSERT-per-second performance of SQLite?Can I ensure that, when fopen()-ing a file for “w”, the program doesn't create a file?Write lines to a text file during program execution - faster wayfprintf not writing to file, despite fclose()C program not writing to text fileWhy I cannot open another file?C - Create/Write/Read named pipeWrite program that pretends to be a TTYfclose(stdout) vs close(STDOUT_FILENO) - CReleasing a file write lock in C?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I'm trying to write my results to an outputfile, running this C program i Mac Terminal. I have checked that all parts of the program works by writing directly to the terminal, but when I'm trying to write to file, nothing happens.



The "writing to file" line writes on every iteration, however nothing happens to the outputdata.txt file.



I've changed the permissions, and I'm able to write to this file directly from the terminal. However, it doesn't work using the below code.



#define OUTPUTFILE "outputdata.txt"

FILE *ofp;

char ofile_name[50] = OUTPUTFILE;

ofp = fopen(ofile_name, "r");

for (p = 1; p <= NumPattern ; p++)
for (k = 1 ; k <= numnodes_out ; k++)
fprintf(ofp, "%fn", output_nodes[p][k]);
fprintf(stdout, "Writing to filen");


fclose(ofp);









share|improve this question






























    1















    I'm trying to write my results to an outputfile, running this C program i Mac Terminal. I have checked that all parts of the program works by writing directly to the terminal, but when I'm trying to write to file, nothing happens.



    The "writing to file" line writes on every iteration, however nothing happens to the outputdata.txt file.



    I've changed the permissions, and I'm able to write to this file directly from the terminal. However, it doesn't work using the below code.



    #define OUTPUTFILE "outputdata.txt"

    FILE *ofp;

    char ofile_name[50] = OUTPUTFILE;

    ofp = fopen(ofile_name, "r");

    for (p = 1; p <= NumPattern ; p++)
    for (k = 1 ; k <= numnodes_out ; k++)
    fprintf(ofp, "%fn", output_nodes[p][k]);
    fprintf(stdout, "Writing to filen");


    fclose(ofp);









    share|improve this question


























      1












      1








      1








      I'm trying to write my results to an outputfile, running this C program i Mac Terminal. I have checked that all parts of the program works by writing directly to the terminal, but when I'm trying to write to file, nothing happens.



      The "writing to file" line writes on every iteration, however nothing happens to the outputdata.txt file.



      I've changed the permissions, and I'm able to write to this file directly from the terminal. However, it doesn't work using the below code.



      #define OUTPUTFILE "outputdata.txt"

      FILE *ofp;

      char ofile_name[50] = OUTPUTFILE;

      ofp = fopen(ofile_name, "r");

      for (p = 1; p <= NumPattern ; p++)
      for (k = 1 ; k <= numnodes_out ; k++)
      fprintf(ofp, "%fn", output_nodes[p][k]);
      fprintf(stdout, "Writing to filen");


      fclose(ofp);









      share|improve this question
















      I'm trying to write my results to an outputfile, running this C program i Mac Terminal. I have checked that all parts of the program works by writing directly to the terminal, but when I'm trying to write to file, nothing happens.



      The "writing to file" line writes on every iteration, however nothing happens to the outputdata.txt file.



      I've changed the permissions, and I'm able to write to this file directly from the terminal. However, it doesn't work using the below code.



      #define OUTPUTFILE "outputdata.txt"

      FILE *ofp;

      char ofile_name[50] = OUTPUTFILE;

      ofp = fopen(ofile_name, "r");

      for (p = 1; p <= NumPattern ; p++)
      for (k = 1 ; k <= numnodes_out ; k++)
      fprintf(ofp, "%fn", output_nodes[p][k]);
      fprintf(stdout, "Writing to filen");


      fclose(ofp);






      c






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 23 at 12:00









      Jonathon Reinhart

      93.6k21170240




      93.6k21170240










      asked Mar 23 at 11:59









      Jonas Hyllseth RyenJonas Hyllseth Ryen

      112




      112






















          1 Answer
          1






          active

          oldest

          votes


















          4














          You're opening the file in read mode, see https://linux.die.net/man/3/fopen.



          If you want to write to the file you have to open the file with a mode that supports writing, for example: fopen(ofile_name, "w").



          Your primary options if you only want to write to the file are:



          1. "w", which will create the file if it does not exist, otherwise it will truncate the file to 0 length (remove everything in the file) and allow you to write to it; or,

          2. "a", which will append to the end of an existing file.

          Additionally, if you look at the link previously mentioned, you should note that the function could return null if the file does not open successfully. Because of this you should check if the FILE* returned by fopen is not null before operating on it.



          #define OUTPUTFILE "outputdata.txt"

          FILE *ofp;

          char ofile_name[50] = OUTPUTFILE;

          ofp = fopen(ofile_name, "r");

          if (ofp)
          for (p = 1; p <= NumPattern ; p++)
          for (k = 1 ; k <= numnodes_out ; k++)
          fprintf(ofp, "%fn", output_nodes[p][k]);
          fprintf(stdout, "Writing to filen");


          fclose(ofp);






          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55313502%2fcannot-write-to-file-in-c-program%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            4














            You're opening the file in read mode, see https://linux.die.net/man/3/fopen.



            If you want to write to the file you have to open the file with a mode that supports writing, for example: fopen(ofile_name, "w").



            Your primary options if you only want to write to the file are:



            1. "w", which will create the file if it does not exist, otherwise it will truncate the file to 0 length (remove everything in the file) and allow you to write to it; or,

            2. "a", which will append to the end of an existing file.

            Additionally, if you look at the link previously mentioned, you should note that the function could return null if the file does not open successfully. Because of this you should check if the FILE* returned by fopen is not null before operating on it.



            #define OUTPUTFILE "outputdata.txt"

            FILE *ofp;

            char ofile_name[50] = OUTPUTFILE;

            ofp = fopen(ofile_name, "r");

            if (ofp)
            for (p = 1; p <= NumPattern ; p++)
            for (k = 1 ; k <= numnodes_out ; k++)
            fprintf(ofp, "%fn", output_nodes[p][k]);
            fprintf(stdout, "Writing to filen");


            fclose(ofp);






            share|improve this answer





























              4














              You're opening the file in read mode, see https://linux.die.net/man/3/fopen.



              If you want to write to the file you have to open the file with a mode that supports writing, for example: fopen(ofile_name, "w").



              Your primary options if you only want to write to the file are:



              1. "w", which will create the file if it does not exist, otherwise it will truncate the file to 0 length (remove everything in the file) and allow you to write to it; or,

              2. "a", which will append to the end of an existing file.

              Additionally, if you look at the link previously mentioned, you should note that the function could return null if the file does not open successfully. Because of this you should check if the FILE* returned by fopen is not null before operating on it.



              #define OUTPUTFILE "outputdata.txt"

              FILE *ofp;

              char ofile_name[50] = OUTPUTFILE;

              ofp = fopen(ofile_name, "r");

              if (ofp)
              for (p = 1; p <= NumPattern ; p++)
              for (k = 1 ; k <= numnodes_out ; k++)
              fprintf(ofp, "%fn", output_nodes[p][k]);
              fprintf(stdout, "Writing to filen");


              fclose(ofp);






              share|improve this answer



























                4












                4








                4







                You're opening the file in read mode, see https://linux.die.net/man/3/fopen.



                If you want to write to the file you have to open the file with a mode that supports writing, for example: fopen(ofile_name, "w").



                Your primary options if you only want to write to the file are:



                1. "w", which will create the file if it does not exist, otherwise it will truncate the file to 0 length (remove everything in the file) and allow you to write to it; or,

                2. "a", which will append to the end of an existing file.

                Additionally, if you look at the link previously mentioned, you should note that the function could return null if the file does not open successfully. Because of this you should check if the FILE* returned by fopen is not null before operating on it.



                #define OUTPUTFILE "outputdata.txt"

                FILE *ofp;

                char ofile_name[50] = OUTPUTFILE;

                ofp = fopen(ofile_name, "r");

                if (ofp)
                for (p = 1; p <= NumPattern ; p++)
                for (k = 1 ; k <= numnodes_out ; k++)
                fprintf(ofp, "%fn", output_nodes[p][k]);
                fprintf(stdout, "Writing to filen");


                fclose(ofp);






                share|improve this answer















                You're opening the file in read mode, see https://linux.die.net/man/3/fopen.



                If you want to write to the file you have to open the file with a mode that supports writing, for example: fopen(ofile_name, "w").



                Your primary options if you only want to write to the file are:



                1. "w", which will create the file if it does not exist, otherwise it will truncate the file to 0 length (remove everything in the file) and allow you to write to it; or,

                2. "a", which will append to the end of an existing file.

                Additionally, if you look at the link previously mentioned, you should note that the function could return null if the file does not open successfully. Because of this you should check if the FILE* returned by fopen is not null before operating on it.



                #define OUTPUTFILE "outputdata.txt"

                FILE *ofp;

                char ofile_name[50] = OUTPUTFILE;

                ofp = fopen(ofile_name, "r");

                if (ofp)
                for (p = 1; p <= NumPattern ; p++)
                for (k = 1 ; k <= numnodes_out ; k++)
                fprintf(ofp, "%fn", output_nodes[p][k]);
                fprintf(stdout, "Writing to filen");


                fclose(ofp);







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 23 at 12:20

























                answered Mar 23 at 12:10









                JuanJuan

                137113




                137113





























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55313502%2fcannot-write-to-file-in-c-program%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                    Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript