Thread: Pointers are not working correctly

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    3

    Unhappy Pointers are not working correctly

    Hi i'm trying to read data from and archive but, i couldnt, make it here is a sample of my input file (datai want to arrange), output file ( how do i wish my data get arrange), and code output ( how my code is arranging the data)


    Code:
    This is my INPUT DATA  
    ASCII 1
    !File started at: 2008/11/12 13:35:17
     Degrees of freedom: 
     temperature           1  :fs
     Total DOFs:            1
     Number Of Nodes:          25
    Time:       1      1 0.300000000000E-001
    temperature
    Perm:          25          25
              1          1
              2          2
              3          3
              4          4
              5          5
              6          6
              7          7
              8          8
              9          9
             10         10
             11         11
             12         12
             13         13
             14         14
             15         15
             16         16
             17         17
             18         18
             19         19
             20         20
             21         21
             22         22
             23         23
             24         24
             25         25
       10.000000000000000     
       11.000000000000000     
       12.000000000000000     
       13.000000000000000     
       14.000000000000000     
       15.199954991339460     
       16.199954991339780     
       17.199954991338899     
       18.199954991340981     
       19.199954991341123     
       20.370656679269594     
       21.370656679269800     
       22.370656679274532     
       23.370656679277445     
       24.370656679280167     
       25.512291425849234     
       26.512291425852432     
       27.512291425862628     
       28.512291425872711     
       29.512291425879688     
       30.631400793908682     
       31.631400793907517     
       32.631400793922090     
       33.631400793933246     
       34.631400793944735     
    Time:       2      2 0.600000000000E-001
    temperature
    Perm:          25         25
              1          1
              2          2
              3          3
              4          4
              5          5
              6          6
              7          7
              8          8
              9          9
             10         10
             11         11
             12         12
             13         13
             14         14
             15         15
             16         16
             17         17
             18         18
             19         19
             20         20
             21         21
             22         22
             23         23
             24         24
             25         25
       35.000000000000000     
       36.000000000000000     
       37.000000000000000     
       38.000000000000000     
       39.000000000000000     
       40.157362005216520     
       41.157362005217102     
       42.157362005220897     
       43.157362005221138     
       44.157362005235335     
       45.312981987533192     
       46.312981987503875     
       47.312981987483582     
       48.312981987425133     
       49.312981987428635     
       50.466161946464986     
       51.466161946399644     
       52.466161946295564     
       53.466161946179312     
       54.466161946121098     
       55.617384755971983     
       56.617384755900851     
       57.617384755696456     
       58.617384755566832     
       59.617384755436980

    Code:
    This is how do i want my ouptut look alike
              1          1   10.000000000000000        35.000000000000000     
              2          2   11.000000000000000        36.000000000000000     
              3          3   12.000000000000000        37.000000000000000     
              4          4   13.000000000000000        38.000000000000000     
              5          5   14.000000000000000        39.000000000000000     
              6          6   15.199954991339460        40.157362005216520     
              7          7   16.199954991339780        41.157362005217102     
              8          8   17.199954991338899        42.157362005220897     
              9          9   18.199954991340981        43.157362005221138     
             10         10   19.199954991341123        44.157362005235335     
             11         11   20.370656679269594        45.312981987533192     
             12         12   21.370656679269800        46.312981987503875     
             13         13   22.370656679274532        47.312981987483582     
             14         14   23.370656679277445        48.312981987425133     
             15         15   24.370656679280167        49.312981987428635     
             16         16   25.512291425849234        50.466161946464986     
             17         17   26.512291425852432        51.466161946399644     
             18         18   27.512291425862628        52.466161946295564     
             19         19   28.512291425872711        53.466161946179312     
             20         20   29.512291425879688        54.466161946121098     
             21         21   48.312981987425133        55.617384755971983     
             22         22   49.312981987428635        56.617384755900851     
             23         23   50.466161946464986        57.617384755696456     
             24         24   51.466161946399644        58.617384755566832     
             25         25   52.466161946295564        59.617384755436980
    Code:
    This the my program output 
              1          1   10.000000000000000        35.000000000000000     
              2          2   11.000000000000000        36.000000000000000     
              3          3   12.000000000000000        37.000000000000000     
              4          4   13.000000000000000        38.000000000000000     
              5          5   14.000000000000000        39.000000000000000     
              6          6   15.199954991339460        40.157362005216520     
              7          7   16.199954991339780        41.157362005217102     
              8          8   17.199954991338899        42.157362005220897     
              9          9   18.199954991340981        43.157362005221138     
             10         10   19.199954991341123        44.157362005235335     
             11         11   20.370656679269594        45.312981987533192     
             12         12   21.370656679269800        46.312981987503875     
             13         13   22.370656679274532        47.312981987483582     
             14         14   23.370656679277445        48.312981987425133     
             15         15   24.370656679280167        49.312981987428635     
             16         16   25.512291425849234        50.466161946464986     
             17         17   26.512291425852432        51.466161946399644     
             18         18   27.512291425862628        52.466161946295564     
             19         19   28.512291425872711        53.466161946179312     
             20         20   29.512291425879688        54.466161946121098     
             21         21  9277445        48.312981987425133        55.617384755971983     
             15         15   24.370656679280167        49.312981987428635        56.617384755900851     
             16         16   25.512291425849234        50.466161946464986        57.617384755696456     
             17         17   26.512291425852432        51.466161946399644        58.617384755566832     
             18         18   27.512291425862628        52.466161946295564        59.617384755436980

    As you may see, everything seems to be going fine until 21 21 is reached, the first numbers of the second column 9277445 are the last numbers of the second column (line14) , on line 21 the fourth and fifth column are the data that should be on line 22, but somehow 9277445 appear on my data, after 21 the count get lost, and start with 15 instead of 22, 16 instead of 23 and so on...., but the fourth ant five columns are the data that should be on each line.


    Code:
    #include<string.h>
    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    #include<ctype.h>
    #include<io.h>
    
    	FILE *fp;
       FILE *fpx;
       FILE *fpdatos;
       FILE *fpdatosr;
       fpos_t pos;
       char *p ;
       char *q;
       char var1[30];
       char cadena[4096];
       char vacia[4096];
       char cad[4096];
       char *str2="Perm:  ";
       char *str1="Number Of Nodes:";
       int flag=0,i=0,perm=0,fin=0,aux=0,j,hand,cont=0,n;
    
     main()
    	{  //fpos=fopen("posiciones.txt","w");
    
          fpx=fopen("nuevo.result","r");
    		fp=fopen("nuevo.result", "r");
          fpdatos=fopen("salida.txt","w");
          fpdatosr=fopen("salida.txt","r");
    
          n=2;
       		if(fp == NULL)
             	{clrscr();
                 printf("\n Error Archivo no encontrado");
                 getch();
                 return 1;
                 }
             else
             	{		while(fin!=1)
                	  	//while(eof)
                        {   //i++;
                         do
                         {
                			fgets(cadena, 4096, fp);
                         fgets(cad, 4096,fpx);
                         
    
                          p=strstr(cadena,str1);
                         	if(p != NULL)                             // Determinación Número de Nodos
                         		{	printf("\n %s",cadena);
                                  strcpy(var1,&(p[strlen(p)-4]));
                                  perm=atoi(var1);                    //Asigna No. de Nodos a perm
    
                               }
    
                                 p=strstr(cadena,str2);               //Ubica La cadena Perm:
                               if(p !=NULL)                           //Para empezar a leer datos y escribirlos
                               	  {
                            	 while(i<perm)
            										{	fgets(cad,4096,fpx);
         											i++;
           										 }
                                     flag=1;
                                     i=0;
                                    }
    
                                if(flag!=0)     Here on is  where i read data and assign them to the correct line
                                      { do
                                             	{
                                                if(!aux)
                                                	{//cadena[0]='\0';
                                                  memset(cadena,'\0',4096);
                                                  memset(cad,'\0',4096);
                                                  fgetpos(fp, &pos);
                                                  q=fgets(cadena, 4096, fp);
                                                  fgetpos(fp, &pos);                //Lectura de Archivo y escritura
                                                	 q[strlen(q)-1]='\0';
                                                	 p=fgets(cad,4096,fpx);
                                                  fputs(q,fpdatos);
                                                	 fputs(p,fpdatos);
                                                  strcpy(q,"");
                                                  strcpy(p,"");
                                                	 i++;
                                               }
                                                 else
                                                  {
    
                                                  memset(cadena,'\0',4096);
                                                  memset(cad,'\0',4096);
                                                  fgetpos(fpdatosr, &pos);
                                                  q=fgets(cadena, 4096, fpdatosr);
                                                  fgetpos(fpdatosr, &pos);
                                                  q[strlen(q)-1]='\0';
                                                  p=fgets(cad,4096,fpx);
                                                  fputs(q,fpdatos);
                                                  fputs(p,fpdatos);
                                                  strcpy(q,"");
                                                  strcpy(p,"");
                		                            i++;
                                                  }
    
    
                                              }
                                              while((perm) >i);
    
                                              i=0;
                                              while(i<perm)
            			 {fgets(cadena,4096,fp);
           		                    i++;
           			   }
                                              aux=1;
                                              i=0;
                                              flag=0;
                                              cont++;
                                              rewind(fpdatos);
                                              rewind(fpdatosr);
    
                                            }
    
                          }while(cont<n);
                          fin=1;
                         }
    
    
                }
    
    
                fclose(fpx);
                fclose(fp);
                fclose(fpdatos);
                fclose(fpdatosr);
    
    	}


    I know i'm asking for too much, maybe someone have experimented something like me, any sugestion would be very helpful.
    Why my program keeps doing it correctly until 21 is reached.
    As you may see i clear data twice on the same routine just to make sure undesired data appear.

    In Advance Thank you very much!!!!

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    You should really consider posting short codes and isolating your problem.
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by execute View Post
    You should really consider posting short codes and isolating your problem.
    Complete code is better than the "redacted" code people typically post, which invariably omits the relevant sections. Some cboard members like to wade through code, some don't. Either way the chances of an answer are higher when all the code is there.

    EDIT: Of course, if you can boil your problem down to a short and complete example which demonstrates the issue, that's appreciated. But not a prerequisite, IMHO.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    As an advise, always open once every file. Having two fopens on the same file confuses things without a reason. You can just open the input, read everything and store it, edit your information and then save them to an output file at this order. That will make it easier for you.

    If you do one one thing carefully this shouldn't be difficult. You probably have a small bug since it almost works. But it is better to have a simpler logic in your code.

    Indent your code first so it will be readable and I am sure the bug will be spotted

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Seconding brewbuck, that's not so much code to wade through that there's a problem. What it needs is some indentation, because I don't want to fix it and it's annoying enough to try to find what's going on without it that I gave up.

  6. #6
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    One thing which occurs to me is why it goes wrong at 21, what is so special about 21?
    I was thinking decimal and hex conversions.

    Anyway 10 in hex = 16 in decimal
    And 16 hex = 22 in decimal (one off)

    Also note you start off again at 15 which is one off 10 in hex.

    So....couuld it be something to do with that are you reading a hex value and writing it as a decimal or vice versa.

    Anyway just an idea.

    Apart from that I would not have done what ever you are trying to do I would have just read
    the file sequentially and buffered what I wanted and then written the output file.
    It's a lot easier that way :O)
    I have no idea why you have so many file pointers, unless you like making things difficult for yourself.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    #include<string.h>
    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>   // remove this non-portable function
    #include<ctype.h>
    #include<io.h>      // no idea why you would want this
    
    // Way too many global variables
    FILE *fp;
    FILE *fpx;
    FILE *fpdatos;
    FILE *fpdatosr;
    fpos_t pos;
    char *p ;
    char *q;
    char var1[30];
    char cadena[4096];
    char vacia[4096];
    char cad[4096];
    char *str2="Perm:  ";
    char *str1="Number Of Nodes:";
    int flag=0,i=0,perm=0,fin=0,aux=0,j,hand,cont=0,n;
    
    main()
    {  //fpos=fopen("posiciones.txt","w");
    
        fpx=fopen("nuevo.result","r");
        fp=fopen("nuevo.result", "r");
        fpdatos=fopen("salida.txt","w");    //!! a read and write of the same file
        fpdatosr=fopen("salida.txt","r");   //!! is a disaster waiting to happen
        //!! only fp gets checked for NULL - why?
        
        n=2;
        if(fp == NULL)
        {
            clrscr();
            printf("\n Error Archivo no encontrado");
            getch();
            return 1;
        }
        else
        {
            while(fin!=1)
            //while(eof)
            {   //i++;
                do
                {
                    //!! if you want to read 2 lines from the same file,
                    //!! then read two lines from the same file.
                    //!! I see no possible reasons for 2 file pointers here
                    //!! also use sizeof(cadena) in place of 4096 (don't use magic numbers)
                    fgets(cadena, 4096, fp);
                    fgets(cad, 4096,fpx);
                    
                    p=strstr(cadena,str1);
                    if(p != NULL)                             // Determinaci&#243;n N&#250;mero de Nodos
                    {	printf("\n &#37;s",cadena);
                        strcpy(var1,&(p[strlen(p)-4]));
                        perm=atoi(var1);                    //Asigna No. de Nodos a perm
                    }
    
                    p=strstr(cadena,str2);               //Ubica La cadena Perm:
                    if(p !=NULL)                           //Para empezar a leer datos y escribirlos
                    {
                        while(i<perm)
                        {
                            fgets(cad,4096,fpx);
                            i++;
                        }
                        flag=1;
                        i=0;
                    }
    
                    if(flag!=0)     Here on is  where i read data and assign them to the correct line
                    {
                        do
                        {
                            if(!aux)
                            {//cadena[0]='\0';
                                memset(cadena,'\0',4096);
                                memset(cad,'\0',4096);
                                fgetpos(fp, &pos);
                                q=fgets(cadena, 4096, fp);
                                fgetpos(fp, &pos);                //Lectura de Archivo y escritura
                                q[strlen(q)-1]='\0';
                                p=fgets(cad,4096,fpx);
                                fputs(q,fpdatos);
                                fputs(p,fpdatos);
                                strcpy(q,"");
                                strcpy(p,"");
                                i++;
                            }
                            else
                            {
                                memset(cadena,'\0',4096);
                                memset(cad,'\0',4096);
                                fgetpos(fpdatosr, &pos);
                                q=fgets(cadena, 4096, fpdatosr);
                                fgetpos(fpdatosr, &pos);
                                q[strlen(q)-1]='\0';
                                p=fgets(cad,4096,fpx);
                                fputs(q,fpdatos);
                                fputs(p,fpdatos);
                                strcpy(q,"");
                                strcpy(p,"");
                                i++;
                            }
                        }
                        while((perm) >i);
    
                        i=0;
                        while(i<perm)
                        {
                            fgets(cadena,4096,fp);
                            i++;
                        }
                        aux=1;
                        i=0;
                        flag=0;
                        cont++;
                        rewind(fpdatos);
                        rewind(fpdatosr);
                    }
                }
                while(cont<n);
                fin=1;
            }
        }
    
        fclose(fpx);
        fclose(fp);
        fclose(fpdatos);
        fclose(fpdatosr);
        return 0;   //!! added
    }
    Step 1 is learn how to format code with some sense of uniformity.

    Added some //!! comments.
    The big problem IMO is all the duplicate file handles you have.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Nov 2008
    Posts
    3

    Smile

    Thank you very much i made it, i followed up what C_ntua and esbo told me, in fact as you may saw i haven't had a course on c or c ++, i really need to earn good knowledge an be cleaner in how i make my programs, just another thing i wasn't looking for someone to understand my code as it is, the fact is that with your experience you gave me the correct way to solve this, and that was what is was looking for.

    GREAT FORUM!!!!

  9. #9
    Registered User
    Join Date
    Nov 2008
    Posts
    3
    Quote Originally Posted by Salem View Post
    Code:
    #include<string.h>
    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>   // remove this non-portable function
    #include<ctype.h>
    #include<io.h>      // no idea why you would want this
    
    // Way too many global variables
    FILE *fp;
    FILE *fpx;
    FILE *fpdatos;
    FILE *fpdatosr;
    fpos_t pos;
    char *p ;
    char *q;
    char var1[30];
    char cadena[4096];
    char vacia[4096];
    char cad[4096];
    char *str2="Perm:  ";
    char *str1="Number Of Nodes:";
    int flag=0,i=0,perm=0,fin=0,aux=0,j,hand,cont=0,n;
    
    main()
    {  //fpos=fopen("posiciones.txt","w");
    
        fpx=fopen("nuevo.result","r");
        fp=fopen("nuevo.result", "r");
        fpdatos=fopen("salida.txt","w");    //!! a read and write of the same file
        fpdatosr=fopen("salida.txt","r");   //!! is a disaster waiting to happen
        //!! only fp gets checked for NULL - why?
        
        n=2;
        if(fp == NULL)
        {
            clrscr();
            printf("\n Error Archivo no encontrado");
            getch();
            return 1;
        }
        else
        {
            while(fin!=1)
            //while(eof)
            {   //i++;
                do
                {
                    //!! if you want to read 2 lines from the same file,
                    //!! then read two lines from the same file.
                    //!! I see no possible reasons for 2 file pointers here
                    //!! also use sizeof(cadena) in place of 4096 (don't use magic numbers)
                    fgets(cadena, 4096, fp);
                    fgets(cad, 4096,fpx);
                    
                    p=strstr(cadena,str1);
                    if(p != NULL)                             // Determinaci&#243;n N&#250;mero de Nodos
                    {	printf("\n &#37;s",cadena);
                        strcpy(var1,&(p[strlen(p)-4]));
                        perm=atoi(var1);                    //Asigna No. de Nodos a perm
                    }
    
                    p=strstr(cadena,str2);               //Ubica La cadena Perm:
                    if(p !=NULL)                           //Para empezar a leer datos y escribirlos
                    {
                        while(i<perm)
                        {
                            fgets(cad,4096,fpx);
                            i++;
                        }
                        flag=1;
                        i=0;
                    }
    
                    if(flag!=0)     Here on is  where i read data and assign them to the correct line
                    {
                        do
                        {
                            if(!aux)
                            {//cadena[0]='\0';
                                memset(cadena,'\0',4096);
                                memset(cad,'\0',4096);
                                fgetpos(fp, &pos);
                                q=fgets(cadena, 4096, fp);
                                fgetpos(fp, &pos);                //Lectura de Archivo y escritura
                                q[strlen(q)-1]='\0';
                                p=fgets(cad,4096,fpx);
                                fputs(q,fpdatos);
                                fputs(p,fpdatos);
                                strcpy(q,"");
                                strcpy(p,"");
                                i++;
                            }
                            else
                            {
                                memset(cadena,'\0',4096);
                                memset(cad,'\0',4096);
                                fgetpos(fpdatosr, &pos);
                                q=fgets(cadena, 4096, fpdatosr);
                                fgetpos(fpdatosr, &pos);
                                q[strlen(q)-1]='\0';
                                p=fgets(cad,4096,fpx);
                                fputs(q,fpdatos);
                                fputs(p,fpdatos);
                                strcpy(q,"");
                                strcpy(p,"");
                                i++;
                            }
                        }
                        while((perm) >i);
    
                        i=0;
                        while(i<perm)
                        {
                            fgets(cadena,4096,fp);
                            i++;
                        }
                        aux=1;
                        i=0;
                        flag=0;
                        cont++;
                        rewind(fpdatos);
                        rewind(fpdatosr);
                    }
                }
                while(cont<n);
                fin=1;
            }
        }
    
        fclose(fpx);
        fclose(fp);
        fclose(fpdatos);
        fclose(fpdatosr);
        return 0;   //!! added
    }
    Step 1 is learn how to format code with some sense of uniformity.

    Added some //!! comments.
    The big problem IMO is all the duplicate file handles you have.
    Salem Thank you very much too, with your comments make my understand that my program had no uniformity and sense. I wrote my code again and finally make it work!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Working with pointers...
    By CompiledMonkey in forum C++ Programming
    Replies: 2
    Last Post: 05-09-2005, 08:14 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. moving pointers to pointers
    By Benzakhar in forum C++ Programming
    Replies: 9
    Last Post: 12-27-2003, 08:30 AM
  5. delete and delete[]
    By Hunter2 in forum C++ Programming
    Replies: 13
    Last Post: 06-26-2003, 04:40 AM