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. simple program

simple program

Scheduled Pinned Locked Moved C / C++ / MFC
question
10 Posts 6 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

    Why does this program crush?, what have i left? #include "stdio.h" int main(int argc, char* argv[]) { FILE* fp; char fname[10]; printf("Enter the file name: ",fname); scanf ("%s",fname); if( fp ) fopen("&fname","wt"); { int i; for( i=0; i<10; ++i ) fprintf(fp,"Line %d\n",i); fclose(fp); } return 0; } oam

    C E D 3 Replies Last reply
    0
    • M mpapeo

      Why does this program crush?, what have i left? #include "stdio.h" int main(int argc, char* argv[]) { FILE* fp; char fname[10]; printf("Enter the file name: ",fname); scanf ("%s",fname); if( fp ) fopen("&fname","wt"); { int i; for( i=0; i<10; ++i ) fprintf(fp,"Line %d\n",i); fclose(fp); } return 0; } oam

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      you never assigned a value to fp. fp = fopen(...); Image Toolkits | Image Processing | Cleek

      1 Reply Last reply
      0
      • M mpapeo

        Why does this program crush?, what have i left? #include "stdio.h" int main(int argc, char* argv[]) { FILE* fp; char fname[10]; printf("Enter the file name: ",fname); scanf ("%s",fname); if( fp ) fopen("&fname","wt"); { int i; for( i=0; i<10; ++i ) fprintf(fp,"Line %d\n",i); fclose(fp); } return 0; } oam

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

        mpapeo wrote: if( fp ) fopen("&fname","wt"); { int i; for( i=0; i<10; ++i ) fprintf(fp,"Line %d\n",i); fclose(fp); } it's should be :

        fopen(&fname,"wt"); //&fname without quotation marks
        if( fp )
        {
        int i;
        for( i=0; i<10; ++i )
        fprintf(fp,"Line %d\n",i);
        fclose(fp);
        }

        R M 2 Replies Last reply
        0
        • E eli15021979

          mpapeo wrote: if( fp ) fopen("&fname","wt"); { int i; for( i=0; i<10; ++i ) fprintf(fp,"Line %d\n",i); fclose(fp); } it's should be :

          fopen(&fname,"wt"); //&fname without quotation marks
          if( fp )
          {
          int i;
          for( i=0; i<10; ++i )
          fprintf(fp,"Line %d\n",i);
          fclose(fp);
          }

          R Offline
          R Offline
          Ravi Bhavnani
          wrote on last edited by
          #4

          Actually, **fp =** fopen (...);, but you probably meant that. /ravi My new year's resolution: 2048 x 1536 Home | Articles | Freeware | Music ravib@ravib.com

          E 1 Reply Last reply
          0
          • E eli15021979

            mpapeo wrote: if( fp ) fopen("&fname","wt"); { int i; for( i=0; i<10; ++i ) fprintf(fp,"Line %d\n",i); fclose(fp); } it's should be :

            fopen(&fname,"wt"); //&fname without quotation marks
            if( fp )
            {
            int i;
            for( i=0; i<10; ++i )
            fprintf(fp,"Line %d\n",i);
            fclose(fp);
            }

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

            Still it crushes oam because i have made the changes you suggested. #include "stdio.h" int main(int argc, char* argv[]) { FILE* fp; char fname[30]; printf("Enter the file name: ",fname); scanf ("%s",fname); fopen(&fname,"wt"); if( fp ) { int i; for( i=0; i<10; ++i ) fprintf(fp,"Line %d\n",i); fclose(fp); } return 0; }

            M 1 Reply Last reply
            0
            • M mpapeo

              Still it crushes oam because i have made the changes you suggested. #include "stdio.h" int main(int argc, char* argv[]) { FILE* fp; char fname[30]; printf("Enter the file name: ",fname); scanf ("%s",fname); fopen(&fname,"wt"); if( fp ) { int i; for( i=0; i<10; ++i ) fprintf(fp,"Line %d\n",i); fclose(fp); } return 0; }

              M Offline
              M Offline
              marinme
              wrote on last edited by
              #6

              you still have yet to assign fp as was stated above

              M 1 Reply Last reply
              0
              • M marinme

                you still have yet to assign fp as was stated above

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

                Well it works now. Thanks oam

                1 Reply Last reply
                0
                • R Ravi Bhavnani

                  Actually, **fp =** fopen (...);, but you probably meant that. /ravi My new year's resolution: 2048 x 1536 Home | Articles | Freeware | Music ravib@ravib.com

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

                  right :doh:

                  1 Reply Last reply
                  0
                  • M mpapeo

                    Why does this program crush?, what have i left? #include "stdio.h" int main(int argc, char* argv[]) { FILE* fp; char fname[10]; printf("Enter the file name: ",fname); scanf ("%s",fname); if( fp ) fopen("&fname","wt"); { int i; for( i=0; i<10; ++i ) fprintf(fp,"Line %d\n",i); fclose(fp); } return 0; } oam

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

                    mpapeo wrote: if( fp ) fopen("&fname","wt"); { Should be:

                    fp = fopen(fname, "wt");
                    if (NULL != fp)
                    {
                    ...


                    "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

                      mpapeo wrote: if( fp ) fopen("&fname","wt"); { Should be:

                      fp = fopen(fname, "wt");
                      if (NULL != fp)
                      {
                      ...


                      "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
                      #10

                      Thanks. 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