Short program

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JasBascom

    Short program

    I have written a program that is suppose to compare to lines
    c12345
    I04567

    the program takes the first digit encountered of both lines, decides which one
    is smaller and writes that to a file called sorted data. the problem is that
    that file (a text file) won't create. Can you tell me what if anything i'm
    doing wrong in the program, and whether i can force the program to create the
    file (sorted data)


    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <cstring>
    #include <cstdlib>


    using namespace std;

    struct crecord {
    char customercode[5];
    char customername[21];
    char customeraddress[61];
    char customerbalance ;
    char creditlimit;
    int Totalbalance;
    int Totalcreditlimi t;

    };

    struct irrecord {
    char customercode[5];
    char partnum[6];
    char issue_rec[5];

    };


    struct drecord {
    char customercode[5];
    };

    int loop = 200;
    long offset = 1;

    union Allrecords{
    struct crecord Newcrecord;
    struct irrecord Newirrecord;
    struct drecord Newdrecord;

    };
    union Allrecords unionarray;





    void sort_function( union Allrecords unionarray, ifstream& validdata, char*
    temp2 )
    {

    union Allrecords *str_ptr1, *str_ptr2, tempstr;




    for(int i =0; i< loop; i++)
    while( strcmp(str_ptr1[i].Newcrecord.cus tomercode, '\0') ||
    strcmp(str_ptr1[i].Newdrecord.cus tomercode, '\0') ||
    strcmp(str_ptr1[i].Newirrecord.cu stomercode, '\0'))
    {
    str_ptr2 = str_ptr1 + 1;//set to next element.

    for( i=0; i<loop; i++)
    while( strcmp(str_ptr2[i].Newcrecord.cus tomercode, '\0') ||
    strcmp(str_ptr2[i].Newdrecord.cus tomercode, '\0'))
    {
    for(int i=0; i<loop; i++)
    if( strcmp( str_ptr1[i].Newirrecord.cu stomercode,
    str_ptr2[i].Newirrecord.cu stomercode + 1))
    {
    tempstr = *str_ptr1;
    *str_ptr1 = *str_ptr2;
    *str_ptr2 = tempstr;

    }
    *str_ptr1++;//incremented, so that the same code isn't sorted again
    }
    str_ptr2++;
    }

    }









    int main()
    {
    const char sorted_file[] = "A:\\514650P2SD .txt";
    const char outfile[] = "A:\\514650VDP1 .bin";



    union Allrecords unionarray;

    char* characterarray;
    long offset = 1, end_of_file = 0;
    int index = 0;


    ifstream sort_file;
    ifstream validdata;


    sort_file.open( "A:\\514650P2SD .txt", ios::in);
    if(!sort_file)
    {
    cout<<"Cannot create file"<< endl;
    return EXIT_FAILURE;
    }

    validdata.open( "A:\\514650VDP1 .bin", ios::in || ios::binary);
    if(!validdata)
    {
    cout<<" Cannot find file"<<endl;
    return EXIT_FAILURE;
    }


    validdata.seekg (0,ios::end);
    end_of_file = validdata.tellg ();

    while(offset <= end_of_file)
    {
    validdata.seekg (-offset, ios::end);
    characterarray[index++] = validdata.get() ;

    offset++;
    }


    while(sort_file .peek() != EOF)//read the whole file.
    {
    sort_file.getli ne( characterarray, sizeof(characte rarray[index]) );

    switch(characte rarray[4])
    {
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':
    case '0':
    sort_function(u nionarray, validdata, characterarray) ;
    default:;
    }

    }

    return 0;

    }




  • John Harrison

    #2
    Re: Short program


    "JasBascom" <jasbascom@aol. com> wrote in message
    news:2004021515 5254.28383.0000 2080@mb-m01.aol.com...[color=blue]
    > I have written a program that is suppose to compare to lines
    > c12345
    > I04567
    >
    > the program takes the first digit encountered of both lines, decides which[/color]
    one[color=blue]
    > is smaller and writes that to a file called sorted data.[/color]

    Here you say that you want to write sorted_data.
    [color=blue]
    > the problem is that
    > that file (a text file) won't create. Can you tell me what if anything i'm
    > doing wrong in the program, and whether i can force the program to create[/color]
    the[color=blue]
    > file (sorted data)
    >
    >[/color]

    [snip]
    [color=blue]
    > ifstream sort_file;[/color]

    Here you declare the sorted file for input.
    [color=blue]
    > ifstream validdata;
    >
    >
    > sort_file.open( "A:\\514650P2SD .txt", ios::in);[/color]

    And here you open it for reading.

    [snip]
    [color=blue]
    >
    >
    > while(sort_file .peek() != EOF)//read the whole file.
    > {
    > sort_file.getli ne( characterarray, sizeof(characte rarray[index]) );[/color]

    And here you start reading from it.

    See the problem? You say you want to create/write the sorted file, but all
    the code ever does is read. You need to decide what you actually want to do
    and adjust the code.

    Although you call this a short program, it looks a little complex for you to
    handle. I would strongly recommend breaking it down and trying to do
    something simpler. Its much easier to take a working program and add things
    to it, than it is to make a complex but broken program work. Trust me on
    this, you'll save yourself hours of frustration.

    john


    Comment

    • David Harmon

      #3
      Re: Short program

      On 15 Feb 2004 20:52:54 GMT in comp.lang.c++, jasbascom@aol.c om
      (JasBascom) was alleged to have written:[color=blue]
      > const char sorted_file[] = "A:\\514650P2SD .txt";[/color]
      [][color=blue]
      > ifstream sort_file;[/color]
      [][color=blue]
      > sort_file.open( "A:\\514650P2SD .txt", ios::in);
      > if(!sort_file)
      > {
      > cout<<"Cannot create file"<< endl;
      > return EXIT_FAILURE;
      > }[/color]

      You are sure that the filesystem on A: can support 10-char file names?
      Just to check, try something shorter.

      Suggested changes:

      const char sorted_file[] = "A:\\514650P2SD .txt";
      ifstream sort_file(sorte d_file);
      if(!sort_file)
      {
      perror(sorted_f ile);
      return EXIT_FAILURE;
      }

      Comment

      • David Harmon

        #4
        Re: Short program

        On Sun, 15 Feb 2004 21:20:18 GMT in comp.lang.c++, David Harmon
        <source@netcom. com> was alleged to have written:[color=blue]
        > ifstream sort_file(sorte d_file);[/color]

        Geez, should be ofstream of course.

        Comment

        Working...