Wats the problem with my code?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • flavourofbru
    New Member
    • May 2007
    • 48

    Wats the problem with my code?

    Hi,

    I am reading a text file which has 4 columns into a 3D array.

    My code is compiling...... ..but cant see the output. Shows .exe file stopped working.

    My code is as follows:

    //#include "stdafx.h"
    #include<iostre am>
    #include<string >
    #include<fstrea m>
    using namespace std;

    int function(const char *fName, int img_3D[][128][128])
    {
    int a[5],i,j;
    FILE *fp=fopen("abc. txt","r");
    while(!feof(fp) )
    {
    cout<<"file opened";
    for(i=0;i<4;i++ )
    fscanf(fp,"%d", &a[i]);
    if(feof(fp)) break;
    if(a[0]>126||a[1]>126||a[2]>126)
    {
    printf("Error in input");
    return(-1);
    }
    //printf("%d %d %d %d\n",a[0],a[1],a[2],a[3]);
    img_3D[a[0]+1][a[1]+1][a[2]+1]=a[3];
    }
    fclose(fp);
    }

    int print(const char *fName,int img_3D[][128][128])
    {
    int a[5],i,j,fee;
    FILE *fp=fopen("abc. txt","r");
    while(!feof(fp) )
    {
    for(i=0;i<4;i++ )
    fscanf(fp,"%d", &a[i]);
    if(feof(fp)) break;
    if(a[0]>126||a[1]>126||a[2]>126)
    {
    printf("Error in input");
    return(-1);
    }
    printf("%d\n",i mg_3D[a[0]+1][a[1]+1][a[2]+1]);
    }
    fclose(fp);
    }

    int main()
    {
    int a[128][128][128];
    function("abc.t xt",a);
    print("abc.txt" ,a);
    return 0;
    }

    In my text file, I will be skipping the first row, then start reading from the 2nd row. My text files contains a set of integers.

    Though its compiling, why cant I see the output?

    Pls help.

    Thanks!!
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #2
    If you're using an IDE (eg dev-cpp etc) then you might find that the code is working, but the window is not staying around long enough for you to see it.

    You could add a big loop at the bootom to delay the window, or you could add a command to wait for user input before closing (eg system("pause") ;)

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by flavourofbru
      Hi,

      I am reading a text file which has 4 columns into a 3D array.

      My code is compiling...... ..but cant see the output. Shows .exe file stopped working.

      My code is as follows:

      //#include "stdafx.h"
      #include<iostre am>
      #include<string >
      #include<fstrea m>
      using namespace std;

      int function(const char *fName, int img_3D[][128][128])
      {
      int a[5],i,j;
      FILE *fp=fopen("abc. txt","r");
      while(!feof(fp) )
      {
      cout<<"file opened";
      for(i=0;i<4;i++ )
      fscanf(fp,"%d", &a[i]);
      if(feof(fp)) break;
      if(a[0]>126||a[1]>126||a[2]>126)
      {
      printf("Error in input");
      return(-1);
      }
      //printf("%d %d %d %d\n",a[0],a[1],a[2],a[3]);
      img_3D[a[0]+1][a[1]+1][a[2]+1]=a[3];
      }
      fclose(fp);
      }

      int print(const char *fName,int img_3D[][128][128])
      {
      int a[5],i,j,fee;
      FILE *fp=fopen("abc. txt","r");
      while(!feof(fp) )
      {
      for(i=0;i<4;i++ )
      fscanf(fp,"%d", &a[i]);
      if(feof(fp)) break;
      if(a[0]>126||a[1]>126||a[2]>126)
      {
      printf("Error in input");
      return(-1);
      }
      printf("%d\n",i mg_3D[a[0]+1][a[1]+1][a[2]+1]);
      }
      fclose(fp);
      }

      int main()
      {
      int a[128][128][128];
      function("abc.t xt",a);
      print("abc.txt" ,a);
      return 0;
      }

      In my text file, I will be skipping the first row, then start reading from the 2nd row. My text files contains a set of integers.

      Though its compiling, why cant I see the output?

      Pls help.

      Thanks!!
      1.) Code tags code tags code tags code tags code tags code tags code tags
      2.) You can run it from the command. Just get to the folder with the executable and run it from there.

      Comment

      Working...