Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi Everyone,

As a continuation of one of my previous questions...URL below
alphanumeric increment in C[^]

Using the above working code, I open a file, use fgets and sscanf to scan the rows. Each row has a number of items, one of which is a value of 6 characters. The code then "renumbers" it down to 4 characters and prints it out to an output file. What I need to do now is the following:
1. open another, different file, call it FILE2, that also contains the same the 6 character values in a different layout
2. I need to scan FILE2, until I find the one of the 6 character values
3. Then I need to go to the FILE1, find the same 6 Character value and find and store it's matching 4 character value
4. Go back to FILE2 replace the 6 character value it with it's equivalent 4 character value.

Original code snippit is below:
C#
// CProgtest.cpp : main project file.
#include <stdAfx.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include "CProgtest.h"
int main()
{
    //
    //
    char *pdest;
    char t0[50] = "**";
    char t1[50] = "COOR";
    //
    char lnLabel[50], lLabel[50], nLabel[50], line[200];
    long i, j, k, l10, l20, l30, l40, l50, l60, l70, l80, l90;
    long  n10[2000], n20[2000], n30[2000], n40[2000], n50[2000], n60[2000];
    long  n70[2000], n80[2000], n90[2000];
    long NodeName;
    double xA, xB, yA, yB, zA, zB;
    double x, y, z;
    //
    Operations P;
    //
    for (k = 0; k < 2000; k++)
    {
        n10[k] = 0; n20[k] = 0; n30[k] = 0; n40[k] = 0; n50[k] = 0; n60[k] = 0; n70[k] = 0; n80[k] = 0; n90[k] = 0;
    }
    l10 = 10; l20 = 20; l30 = 30; l40 = 40; l50 = 50; l60 = 60; l70 = 70; l80 = 80; l90 = 90;
    FILE *pFile, *sacsout;
    pFile = fopen("ASASIN.txt", "r");
    if( (sacsout = fopen("Sacs1.inp", "w" )) != NULL )
    {
    }
    else
    {printf( "Problem creating SACS Output file\n");}
    //fgets gets a string from a file//one string per line
    while(fgets(line, sizeof line, pFile) != NULL)
    {
       if(sscanf_s(line, "%d %lf %lf %lf", &NodeName, &x, &y, &z) == 4)
            //
            fprintf(sacsout,"JOINT ");
        {
            //Each case below populates the Nodename field with the first two numbers of the ASAS nodename and then increments for the following two numbers up until new number is reached.
            j = NodeName / 10000;
            if(j < 20 )
            {
                if (j != l10 )
                {
                    l10 = j;
                    /*n10 = 0;*/
                }
                n10[l10] += 1;
                fprintf(sacsout,"%2d", l10);
                fprintf(sacsout,"%02lX", n10[l10]);
                sprintf(lLabel,"%d", l10);
                sprintf(nLabel, "%02lX", n10[l10]);
                strcpy(lnLabel, lLabel);
                strcat(lnLabel, nLabel);
                goto skp;
            }
            if(j < 30)
            {
                if( j != l20)
                {
                    l20 = j;
                    /*n20 = 0;*/
                }
                n20[l20] += 1;
                fprintf(sacsout,"%2d", l20);
                fprintf(sacsout,"%02lX", n20[l20]);
                sprintf(lLabel,"%d", l20);
                sprintf(nLabel, "%02lX", n20[l20]);
                strcpy(lnLabel, lLabel);
                strcat(lnLabel, nLabel);
                goto skp;
            }
            if(j < 40)
            {
                if( j != l30)
                {
                    l30 = j;
                    /*n30 = 0;*/
                }
                n30[l30] += 1;
                fprintf(sacsout,"%2d", l30);
                fprintf(sacsout,"%02lX", n30[l30]);
                sprintf(lLabel,"%d", l30);
                sprintf(nLabel, "%02lX", n30[l30]);
                strcpy(lnLabel, lLabel);
                strcat(lnLabel, nLabel);
                goto skp;
            }
            if(j < 50)
            {
                if( j != l40)
                {
                    l40 = j;
                    /*n40 = 0;*/
                }
                n40[l40] += 1;
                fprintf(sacsout,"%2d", l40);
                fprintf(sacsout,"%02lX", n40[l40]);
                sprintf(lLabel,"%d", l40);
                sprintf(nLabel, "%02lX", n40[l40]);
                strcpy(lnLabel, lLabel);
                strcat(lnLabel, nLabel);
                goto skp;
            }
            if(j < 60)
            {
                if( j != l50)
                {
                    l50 = j;
                    /*n40 = 0;*/
                }
                n50[l50] += 1;
                fprintf(sacsout,"%2d", l50);
                fprintf(sacsout,"%02lX", n50[l50]);
                sprintf(lLabel,"%d", l50);
                sprintf(nLabel, "%02lX", n50[l50]);
                strcpy(lnLabel, lLabel);
                strcat(lnLabel, nLabel);
                goto skp;
            }
            if(j < 70)
            {
                if( j != l60)
                {
                    l60 = j;
                    /*n60 = 0;*/
                }
                n60[l60] += 1;
                fprintf(sacsout,"%2d", l60);
                fprintf(sacsout,"%02lX", n60[l60]);
                sprintf(lLabel,"%d", l60);
                sprintf(nLabel, "%02lX", n60[l60]);
                strcpy(lnLabel, lLabel);
                strcat(lnLabel, nLabel);
                goto skp;
            }
            if(j < 80 )
            {
                if (j != l70 )
                {
                    l70 = j;
                    /*n70 = 0;*/
                }
                n70[l70] += 1;
                fprintf(sacsout,"%2d", l70);
                fprintf(sacsout,"%02lX", n70[l70]);
                sprintf(lLabel,"%d", l70);
                sprintf(nLabel, "%02lX", n70[l70]);
                strcpy(lnLabel, lLabel);
                strcat(lnLabel, nLabel);
                goto skp;
            }
            if(j < 90 )
            {
                if (j != l80 )
                {
                    l80 = j;
                    /*n80 = 0;*/
                }
                n80[l80] += 1;
                fprintf(sacsout,"%2d", l80);
                fprintf(sacsout,"%02lX", n80[l80]);
                sprintf(lLabel,"%d", l80);
                sprintf(nLabel, "%02lX", n80[l80]);
                strcpy(lnLabel, lLabel);
                strcat(lnLabel, nLabel);
                goto skp;
            }
            if(j < 100 )
            {
                if (j != l90 )
                {
                    l90 = j;
                    /*n90 = 0;*/
                }
                n90[l90] += 1;
                fprintf(sacsout,"%2d", l90);
                fprintf(sacsout,"%02lX", n90[l90]);
                sprintf(lLabel,"%d", l90);
                sprintf(nLabel, "%02lX", n90[l90]);
                strcpy(lnLabel, lLabel);
                strcat(lnLabel, nLabel);
                goto skp;
            }
skp:;
//The rest of the code goes here
}
Posted
Updated 9-May-13 23:54pm
v3
Comments
CPallini 10-May-13 4:39am    
The operation you want to apply on the second file looks identitical to the one you apply to the first one, or am I wrong?
Member 9357265 10-May-13 4:49am    
I need to make sure that, for example, if I renamed 102030 to 1015 on one file, I need to find 102030 on the other file and rename it to 1015. The two files are used for two different functions, but they use the same content for certain components.
Sergey Alexandrovich Kryukov 10-May-13 9:52am    
If you are going to write code in this way, you will never get anything satisfactory. Everything is wrong. You just repeat nearly identical fragments of code again and again, for different ranges. This is not programming at all, not even close.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900