Fstream problem

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

    Fstream problem

    I'm having 2 problems and hope you can help:

    Fist Problem: Please see the code segment bellow. For this application I need
    to access an input file and also an output file, however, when I include the
    statements to access to access the output file, I get an access violation
    error. When I comment out the output file statements, the program runs fine.

    Code segment:

    int main()
    {
    int choice = 1; // To store menu option selected by user
    ifstream infile; // input file access
    // ofstream outfile;
    char* inputFile; // input file
    char* outputFile; // outputFile
    NodePtr head; // External pointer to head of linked
    list
    NodePtr currPtr; // Points to current node
    NodePtr newNodePtr; // points to newest node
    char noun[30], partNum[20], locationID[10]; // temp storage for file
    input data
    int qtyInStock, restockL; // " " "

    inputFile = "mystock.tx t";
    outputFile = "update.txt ";
    infile.open(inp utFile);
    if(!infile)
    {
    cout << "Could not open file: " << inputFile << endl;
    return 1;
    }
    // outfile.open(ou tputFile);
    // if(!outfile)
    // {
    // cout << "Could not open file: " << outputFile << endl;
    // return 1;
    // }

    SecondProblem : In reading from the intput file, I simply want to say:

    infile >> data;
    while(infile)
    {
    ......
    .......
    infile >> next data;
    }

    When I do it this way, I get stuck in an infinite loop. It reads all the data
    in the file but instead of exiting the loop, it continues to input the last
    line indefinitely. To overcome the problem I've had to enter a '-1' for two
    integer data items on the last line of the file and use that as the test for
    end of file. I need to be able to do it the right way, so please help! Here's
    the code segment:

    infile >> noun >> partNum >> locationID >> qtyInStock >> restockL;
    while((restockL != -1) && (qtyInStock != -1))
    {
    newNodePtr = new SupplyItem;
    strcpy(newNodeP tr->nomenclature , noun);
    strcpy(newNodeP tr->partNumber, partNum);
    strcpy(newNodeP tr->locID,location ID);
    newNodePtr->quantityInStoc k = qtyInStock;
    newNodePtr->restockLevel = restockL;
    currPtr->link = newNodePtr;
    currPtr = newNodePtr;
    infile >> noun >> partNum >> locationID >> qtyInStock >> restockL;
    }
    currPtr->link = NULL;

    Thanks for taking the time.

    Josh

  • Jacek Dziedzic

    #2
    Re: Fstream problem

    "jbruno4000 " <jbruno4000@aol .com> wrote in message
    news:2003110211 1722.26622.0000 0080@mb-m27.aol.com...[color=blue]
    > I'm having 2 problems and hope you can help:
    > [...]
    > I get an access violation error.[/color]

    Definitely.
    [color=blue]
    > When I comment out the output file statements, the
    > program runs fine.[/color]

    By sheer chance. You forget to allocate memory for
    your inputFile and outputFile character strings. By saying
    "char* inputFile" (same for outputFile) you declare a
    pointer to a char, nothing more. You should use arrays
    like in your latter variables like noun[30], or, better,
    switch to std::string's instead.

    Also you can't assign (C-style) strings with "=", like
    you do in inputFile="myst ock.txt". What you're doing
    now is copying pointers to (I think) temporaries, not the
    strings themselves as you have probably intended.

    The bottom line is... read on std::strings or on
    C-style strings (null-terminated character arrays).

    HTH,
    - J.


    Comment

    • Artie Gold

      #3
      Re: Fstream problem

      Jacek Dziedzic wrote:[color=blue]
      > "jbruno4000 " <jbruno4000@aol .com> wrote in message
      > news:2003110211 1722.26622.0000 0080@mb-m27.aol.com...
      >[color=green]
      >>I'm having 2 problems and hope you can help:
      >>[...]
      >>I get an access violation error.[/color]
      >
      >
      > Definitely.
      >
      >[color=green]
      >>When I comment out the output file statements, the
      >>program runs fine.[/color]
      >
      >
      > By sheer chance. You forget to allocate memory for
      > your inputFile and outputFile character strings. By saying
      > "char* inputFile" (same for outputFile) you declare a
      > pointer to a char, nothing more. You should use arrays
      > like in your latter variables like noun[30], or, better,
      > switch to std::string's instead.[/color]

      Erm, that's not correct. The OP was assigning the address of a
      string literal to a char * -- which is not a problem at all.
      [color=blue]
      >
      > Also you can't assign (C-style) strings with "=", like
      > you do in inputFile="myst ock.txt". What you're doing
      > now is copying pointers to (I think) temporaries, not the
      > strings themselves as you have probably intended.[/color]

      See above.
      [color=blue]
      >
      > The bottom line is... read on std::strings or on
      > C-style strings (null-terminated character arrays).[/color]

      HTH,
      --ag

      --
      Artie Gold -- Austin, Texas
      Oh, for the good old days of regular old SPAM.

      Comment

      • Jacek Dziedzic

        #4
        Re: Fstream problem

        "Artie Gold" <artiegold@aust in.rr.com> wrote in message
        news:3FA53AC2.9 020909@austin.r r.com...[color=blue]
        > Jacek Dziedzic wrote:
        > [...]
        >
        > Erm, that's not correct. The OP was assigning the address of a
        > string literal to a char * -- which is not a problem at all.[/color]

        Right, my bad. I somehow thought that the string literal
        dies the next moment, but I guess it doesn't.
        [color=blue][color=green]
        > >
        > > Also you can't assign (C-style) strings with "=", like
        > > you do in inputFile="myst ock.txt". What you're doing
        > > now is copying pointers to (I think) temporaries, not the
        > > strings themselves as you have probably intended.[/color]
        >
        > See above.[/color]

        What I meant is you copy pointers to C-style strings
        that way, not the C-style strings themselves.

        The bottom line is... I "misthought " the string literal
        was a temporary, which makes my reasoning incorrect
        and the original question open. Sorry for the mess.

        - J.


        Comment

        • jbruno4000

          #5
          Re: Fstream problem

          The problem is worst then I thought. I'm using borland 5.5 and I've been having
          problems handling strings, so I entered the simple code bellow and when I run
          it, I get an access violation and it crashes!

          Is it a problem with my compiler? Is it possible somethings wrong with my
          'string.h'? I've done work with strings on this same compiler and there were no
          problems.

          Any ideas?

          ~~~~~~~~~~~~~~~ ~~~~~~~~~

          #include <iostream>
          #include <string>

          using namespace std;

          int main()
          {
          string str_1;
          string str_2;
          string str_3;

          str_1 = "Today";
          str_2 = " is Sunday";
          str_3 = str_1 + str_2;
          cout << str_3 << endl;

          return 0;
          }

          Comment

          • Christian Brechbühler

            #6
            Re: Fstream problem

            jbruno4000 wrote:[color=blue]
            > The problem is worst then I thought. I'm using borland 5.5 and I've been having
            > problems handling strings, so I entered the simple code bellow and when I run
            > it, I get an access violation and it crashes!
            >
            > Is it a problem with my compiler? Is it possible somethings wrong with my
            > 'string.h'? I've done work with strings on this same compiler and there were no
            > problems.
            >
            > Any ideas?
            >
            > ~~~~~~~~~~~~~~~ ~~~~~~~~~
            >
            > #include <iostream>
            > #include <string>
            >
            > using namespace std;
            >
            > int main()
            > {
            > string str_1;
            > string str_2;
            > string str_3;
            >
            > str_1 = "Today";
            > str_2 = " is Sunday";
            > str_3 = str_1 + str_2;
            > cout << str_3 << endl;
            >
            > return 0;
            > }[/color]

            With g++ 3.2, your program compiles and runs fine; it outputs "Today is Sunday
            ". Something must be wrong with your implementation (or installation). BTW, in
            my case, the file is called '/usr/include/c++/3.2/string' -- no '.h'.


            Comment

            Working...