Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. file output

file output

Scheduled Pinned Locked Moved C / C++ / MFC
help
13 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mpapeo
    wrote on last edited by
    #1

    Please help me with a segment code which can redirect the output to a file instead of using >> in the commandline. I am using c language. oam

    E 1 Reply Last reply
    0
    • M mpapeo

      Please help me with a segment code which can redirect the output to a file instead of using >> in the commandline. I am using c language. oam

      E Offline
      E Offline
      eli15021979
      wrote on last edited by
      #2

      Try this:

      FILE *file;
      file = fopen("C:\\Progrem files\\test.txt","w");
      CString output;
      for(int i = 0 ; i < 10 ; i++)
      {
      output.Format("this is test string number %d",i);
      fputs(output.GetBuffer(output.GetLenght() + 1),file);
      }
      fclose(file);

      now,if you will open the test.txt file in your C:\Program files - guess what???:laugh: Good luck, Eli

      M 1 Reply Last reply
      0
      • E eli15021979

        Try this:

        FILE *file;
        file = fopen("C:\\Progrem files\\test.txt","w");
        CString output;
        for(int i = 0 ; i < 10 ; i++)
        {
        output.Format("this is test string number %d",i);
        fputs(output.GetBuffer(output.GetLenght() + 1),file);
        }
        fclose(file);

        now,if you will open the test.txt file in your C:\Program files - guess what???:laugh: Good luck, Eli

        M Offline
        M Offline
        mpapeo
        wrote on last edited by
        #3

        FILE *file; E:\process.c(145) : error C2275: 'FILE' : illegal use of this type as an expression ;) oam

        D J 2 Replies Last reply
        0
        • M mpapeo

          FILE *file; E:\process.c(145) : error C2275: 'FILE' : illegal use of this type as an expression ;) oam

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          Have you included stdio.h? However, if you are going to use MFC, I would construct a CFile object instead.


          "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

          M 1 Reply Last reply
          0
          • D David Crow

            Have you included stdio.h? However, if you are going to use MFC, I would construct a CFile object instead.


            "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

            M Offline
            M Offline
            mpapeo
            wrote on last edited by
            #5

            yes i have included #include stdio.h Im using visual C. How can i construct a struct CFile? oam When opportunity knocks, open the door as it might comes once in a time

            D 1 Reply Last reply
            0
            • M mpapeo

              yes i have included #include stdio.h Im using visual C. How can i construct a struct CFile? oam When opportunity knocks, open the door as it might comes once in a time

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              mpapeo wrote: How can i construct a struct CFile? I (erroneously) assumed you would be using MFC.


              "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

              1 Reply Last reply
              0
              • M mpapeo

                FILE *file; E:\process.c(145) : error C2275: 'FILE' : illegal use of this type as an expression ;) oam

                J Offline
                J Offline
                John R Shaw
                wrote on last edited by
                #7

                This error may be incorrect! The code provided by eli5021979 is correct, accept that it is not competely C code. The CString is an C++ class, get rid of it. Use a char buffer in stead: char buffer[128] (whatever size you need). The error will probably disappear. Heck, in the example code, I would get rid of the temporary buffer and just use fprintf() instead. INTP "The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes." Andrew W. Troelsen

                M 1 Reply Last reply
                0
                • J John R Shaw

                  This error may be incorrect! The code provided by eli5021979 is correct, accept that it is not competely C code. The CString is an C++ class, get rid of it. Use a char buffer in stead: char buffer[128] (whatever size you need). The error will probably disappear. Heck, in the example code, I would get rid of the temporary buffer and just use fprintf() instead. INTP "The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes." Andrew W. Troelsen

                  M Offline
                  M Offline
                  mpapeo
                  wrote on last edited by
                  #8

                  what is the loop for in the above code, i get a lot of errors. Pliz give the idea of how to write the segment code in C oam

                  D J 2 Replies Last reply
                  0
                  • M mpapeo

                    what is the loop for in the above code, i get a lot of errors. Pliz give the idea of how to write the segment code in C oam

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #9

                    mpapeo wrote: what is the loop for in the above code, It executes the two statements within the curly brackets 10 times. mpapeo wrote: i get a lot of errors. And those are?? mpapeo wrote: Pliz give the idea of how to write the segment code in C Hasn't that already been provided?


                    "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                    1 Reply Last reply
                    0
                    • M mpapeo

                      what is the loop for in the above code, i get a lot of errors. Pliz give the idea of how to write the segment code in C oam

                      J Offline
                      J Offline
                      John R Shaw
                      wrote on last edited by
                      #10

                      mpapeo wrote: what is the loop for in the above code For dimenstartion puposes only! It should produce a file containing the following: "this is test string number 0this is test string number 1this is test string number 2this is test string number 3this is test string number 4this is test string number 5this is test string number 6this is test string number 7this is test string number 8this is test string number 9" Note: All variables must be declared at the start of the scope in C programs. You can not do things like for(int i=0;i<10;++i).

                      int DoFileOutTest1()
                      {
                      int i;
                      char buffer[64];
                      FILE *file = fopen("test.txt","w"); // creates or recreates file
                      if( !file )
                      return 0; // failed to create file
                      for(i = 0 ; i < 10 ; ++i) {
                      sprintf(buffer,"this is test string number %d\n",i);
                      fputs(buffer,file);
                      }
                      fclose(file);
                      return 1;
                      }
                      // OR
                      int DoFileOutTest2()
                      {
                      int i;
                      FILE *file = fopen("test.txt","w"); // creates or recreates file
                      if( file )
                      return 0; // failed to create file
                      for(i = 0 ; i < 10 ; ++i) {
                      fprintf(file,,"this is test string number %d\n",i);
                      }
                      fclose(file);
                      return 1;
                      }

                      INTP "The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes." Andrew W. Troelsen

                      M 1 Reply Last reply
                      0
                      • J John R Shaw

                        mpapeo wrote: what is the loop for in the above code For dimenstartion puposes only! It should produce a file containing the following: "this is test string number 0this is test string number 1this is test string number 2this is test string number 3this is test string number 4this is test string number 5this is test string number 6this is test string number 7this is test string number 8this is test string number 9" Note: All variables must be declared at the start of the scope in C programs. You can not do things like for(int i=0;i<10;++i).

                        int DoFileOutTest1()
                        {
                        int i;
                        char buffer[64];
                        FILE *file = fopen("test.txt","w"); // creates or recreates file
                        if( !file )
                        return 0; // failed to create file
                        for(i = 0 ; i < 10 ; ++i) {
                        sprintf(buffer,"this is test string number %d\n",i);
                        fputs(buffer,file);
                        }
                        fclose(file);
                        return 1;
                        }
                        // OR
                        int DoFileOutTest2()
                        {
                        int i;
                        FILE *file = fopen("test.txt","w"); // creates or recreates file
                        if( file )
                        return 0; // failed to create file
                        for(i = 0 ; i < 10 ; ++i) {
                        fprintf(file,,"this is test string number %d\n",i);
                        }
                        fclose(file);
                        return 1;
                        }

                        INTP "The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes." Andrew W. Troelsen

                        M Offline
                        M Offline
                        mpapeo
                        wrote on last edited by
                        #11

                        FILE *file = fopen("test.txt","w"); // creates or recreates file E:\process.c(147) : error C2275: 'FILE' : illegal use of this type as an expression once i declared the FILE i get errors oam

                        J 1 Reply Last reply
                        0
                        • M mpapeo

                          FILE *file = fopen("test.txt","w"); // creates or recreates file E:\process.c(147) : error C2275: 'FILE' : illegal use of this type as an expression once i declared the FILE i get errors oam

                          J Offline
                          J Offline
                          John R Shaw
                          wrote on last edited by
                          #12

                          You need to find a book or sight on C programming. Here: 1) Create and empty console project. 2) Create a new new file named simple.c. 3) Copy the following into simple.c.

                          // simiple.c

                          #include "stdio.h"

                          int main(int argc, char* argv[])
                          {
                          FILE* fp = fopen("test.txt","wt");
                          if( fp )
                          {
                          int i;
                          for( i=0; i<10; ++i )
                          fprintf(fp,"Line %d\n",i);
                          fclose(fp);
                          }
                          return 0;
                          }

                          1. Press F5. 5) Open the file test.txt in you editor to see results. That is all! INTP "The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes." Andrew W. Troelsen
                          M 1 Reply Last reply
                          0
                          • J John R Shaw

                            You need to find a book or sight on C programming. Here: 1) Create and empty console project. 2) Create a new new file named simple.c. 3) Copy the following into simple.c.

                            // simiple.c

                            #include "stdio.h"

                            int main(int argc, char* argv[])
                            {
                            FILE* fp = fopen("test.txt","wt");
                            if( fp )
                            {
                            int i;
                            for( i=0; i<10; ++i )
                            fprintf(fp,"Line %d\n",i);
                            fclose(fp);
                            }
                            return 0;
                            }

                            1. Press F5. 5) Open the file test.txt in you editor to see results. That is all! INTP "The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes." Andrew W. Troelsen
                            M Offline
                            M Offline
                            mpapeo
                            wrote on last edited by
                            #13

                            Well the thing worked and thanks. But now i want to specify the name of the input file on the commandline. oam

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups