Menu

cant get content from file...

2009-03-06
2012-09-26
  • Donnie Hammond

    Donnie Hammond - 2009-03-06

    this is the error that this gives me
    An Access Violation (Segmentation Fault) raised in your program

     
    • Wayne Keen

      Wayne Keen - 2009-03-06

      Divert your warp engine power to the shields.

      Want a better answer, ask a better question. This is probably the worst one I have seen in a long time.

      Do you really expect to just post an error message and get an answer? Withoout us knowing ONE thing about the code?

      Post your Basic 3!

      Wayne

       
    • cpns

      cpns - 2009-03-06

      That's nice for you. It means you code contains an error. What more can we say!?

       
    • Wayne Keen

      Wayne Keen - 2009-03-06

      Stop for a minute and think Donnie. If I told you I wrote a program
      (but did not tell you what the program was, did not post any code)
      and said I was getting a "parse error", could you tell me what
      my specific error was?

      Now, generally, one can guess when one sees a segmentation fault,
      these are usually the result of some form of memory error, something
      like a pointer error, or an array out of bounds condition.

      If you are trying to do some kind of file operations, which your
      subject line implies, then one might surmise you are trying to
      open a file that might not be there, or are doing something with
      the file pointer when the file is closed.

      These are guesses....

      When you just post an error message, and ask what's wrong, it's
      just one step removed from posting "Help, my code won't work, what's
      wrong" and thinking an answer is a reasonable expectation.

      Time taken with things like the Basic 3, describinbg what you are
      trying to do, and what is happening really saves time. It also
      shows respect for those complete strangers that you are asking to
      help you for free.

      Wayne

       
    • Donnie Hammond

      Donnie Hammond - 2009-03-07

      include <iostream>

      include <string>

      include <iomanip>

      include <fstream>

      include <windows.h>

      using namespace std;

      //structures
      struct Genres
      {
          char genres[20];
          char game_title[50];
          Genres *next;
      };
      

      //functions
      void get_info(char game[50] , char genre[20]);
      void display();
      void delete_L();
      void add_node(char game[50] , char genre[20]);
      void move_to_end();
      void find();
      char Gamers[50];
      void delete_node();
      Genres head_ptr;
      Genres
      current_ptr;
      Genres new_ptr;
      Genres
      temp_ptr;
      Genres previous_ptr;
      Genres
      before_ptr;
      Genres after_ptr;
      void search();
      int ready();
      void delete_node(Genres
      previous_ptr);
      char genre[20];
      void find();
      void delete_head();
      char game[50];
      void delete_mid();
      void delete_end();
      int handle(int number);
      int edit(char game[50] , char genre[20]);
      void write_list_to_file();
      void load_list_from_file();
      void insert_node(Genres new_rec_ptr);
      void make_node_new_head(Genres
      new_rec_ptr);
      void add_node_to_end(Genres new_rec_ptr);
      Genres
      pos_insert_point(char Gamers[50]);

      int main()//main is the main part of the program
      {

          int number;
          head_ptr = NULL;
          load_list_from_file();
          do
          {
              cout &lt;&lt; &quot;Enter 0 if you would like to exit the program.\n&quot;;
              cout &lt;&lt; &quot;Enter 1 if you would like to delete something from the list\n&quot;;
              cout &lt;&lt; &quot;Enter 2 if you would like to search the list for something.\n&quot;;
              cout &lt;&lt; &quot;Enter 3 if you would like to add to the list.\n&quot;;
              cout &lt;&lt; &quot;Enter 4 if you would like to display the list.\n&quot;;
              cout &lt;&lt; &quot;Enter choice: &quot;;
              cin &gt;&gt; number;
              cin.ignore(80, '\n');
              handle(number);
          }
          while(number != 0);
          system(&quot;pause&quot;);
      }
      

      void write_list_to_file()
      {
      ofstream outfile;
      outfile.open("GAMES.DAT", ios::out);

          if (outfile)
          {   
              current_ptr = head_ptr;
              if (head_ptr != NULL)
              {
                  do
                  {
                      outfile &lt;&lt; current_ptr-&gt;game_title &lt;&lt; '\n';
                      outfile &lt;&lt; current_ptr-&gt;genres &lt;&lt; '\n';
                      current_ptr = current_ptr-&gt;next;
                  }
                  while(current_ptr != NULL);
                  {
                      outfile &lt;&lt; &quot;End of file.&quot; &lt;&lt; '\n';
                      outfile.close();
                  }
      
              }
          }
          else
          {
              cout &lt;&lt; &quot;Error when opening the file\n&quot;;
      
          }
      
      }
      

      void load_list_from_file()
      {
      Genres *new_rec_ptr;
      ifstream infile;
      int end = 0;
      char temp[50], temp2[20];

      infile.open(&quot;GAMES&quot;,ios::in);
      
      if(infile)
      {
          new_rec_ptr = new Genres;
          do
          {
              if(new_rec_ptr != NULL)
              {
                  infile.get(temp, 50);
                  infile.ignore(80,'\n');
                  infile.get(temp2,20);
                  infile.ignore(80, '\n');
                      if(head_ptr == NULL)
                      {
                          head_ptr = new Genres;
                          strcpy(head_ptr-&gt;game_title, temp);
                          strcpy(head_ptr-&gt;genres, temp2);
                          head_ptr-&gt;next = NULL;
                      }
                      else
                      {
                          add_node(temp, temp2);
                      }
      
              }
              else
              {
                  cout &lt;&lt; &quot;WARNING! Load from disk was not successful.&quot; &lt;&lt; '\n';
                  end = 1;
              }
          }
          while(!infile.eof());
          infile.close();     
      }
      else
      {
          cout &lt;&lt; &quot;No usable data file located. List is empty.\n&quot;;    
      }
      

      }

      void add_node(char game[50] , char genre[20])// this is to add node
       {
      
           new_ptr = new Genres;
      
          strcpy(new_ptr-&gt;game_title , game);
          strcpy(new_ptr-&gt;genres , genre);
          new_ptr-&gt;next = NULL;
      
          move_to_end();
      current_ptr-&gt;next = new_ptr;//DONT FORGET -&gt;NEXT!!!!!!!!!!!!!
      }
      /*void insert_node(Genres *new_rec_ptr)
      {
          if(head_ptr == NULL)
          {
              head_ptr = new Genres;
              new_rec_ptr-&gt;next = NULL;
              head_ptr = new_rec_ptr;
          }
          else
          {   
              if(strcmp(new_rec_ptr-&gt;game_title, head_ptr-&gt;game_title) &lt; 0)
              {
                  make_node_new_head(new_rec_ptr);
              }
              else
              {
                  current_ptr = pos_insert_point(new_rec_ptr-&gt;game_title);
                  before_ptr = current_ptr;
                  after_ptr = current_ptr-&gt;next;
              }
              if (after_ptr == NULL)
              {
              add_node_to_end(new_rec_ptr);
              }
              else
              {
                  before_ptr-&gt;next = new_rec_ptr;
                  new_rec_ptr-&gt;next = after_ptr;
              }
          }   
      }
      

      /
      void make_node_new_head(Genres
      new_rec_ptr)
      {
      Genres *temp_ptr;
      temp_ptr = head_ptr;
      head_ptr = new_rec_ptr;
      }

      void add_node_to_end(Genres *new_rec_ptr)
      {
          new_rec_ptr-&gt;next = NULL;    
          move_to_end();
          current_ptr-&gt;next = new_rec_ptr;
      }
      
      Genres *pos_insert_point(char Gamers[50])
      {
          char temp_game[50];
          Genres *temp_ptr;
          int tempint;
      
          if(head_ptr-&gt;next != NULL)
          {
              current_ptr = head_ptr;
              temp_ptr = current_ptr-&gt;next;
              strcpy(temp_game, temp_ptr-&gt;game_title);
              tempint = strcmp(Gamers, temp_game);
              while((tempint &gt; 0 ) &amp;&amp; (current_ptr-&gt;next != NULL))
              {
                  current_ptr = temp_ptr;
                  temp_ptr = current_ptr-&gt;next;
                  strcpy(temp_game, temp_ptr-&gt;game_title);
                  tempint = strcmp(Gamers, temp_game);
              }
          }
          else
          {
              current_ptr = head_ptr;
          }
          system(&quot;current_ptr&quot;);
      }
      void move_to_end()// for the thing to the end
       {
           current_ptr = head_ptr;
           while(current_ptr-&gt;next != NULL)
           {
               current_ptr= current_ptr-&gt;next;
           }
       }
      void display()// this displays it
      {
          current_ptr = head_ptr;
          cout&lt;&lt; &quot;Game     Genre\n&quot;;
      
          do
          {
      
              cout &lt;&lt; current_ptr-&gt;game_title &lt;&lt; &quot;        &quot;;
      
              cout &lt;&lt; current_ptr-&gt;genres &lt;&lt; &quot;\n&quot;;
              current_ptr=current_ptr -&gt; next;
      
          }while(current_ptr != NULL);
      }
      
      void delete_L()// deletes list
      {
          current_ptr = head_ptr;
          do
          {
              temp_ptr = current_ptr-&gt;next;
      
              delete current_ptr;
              current_ptr = temp_ptr;
          }
          while(temp_ptr !=NULL);
      }
      
      void get_info(char genre[20], char game[50] )// get information
      { 
      Genres *new_rec_ptr;
      new_rec_ptr = new Genres;
      if(new_ptr != NULL)
      {
          cout &lt;&lt; &quot;\n Enter new game\n&quot;;
          cout &lt;&lt; &quot;Game: &quot;;
          cin.get(new_rec_ptr-&gt;game_title,50);
          cin.ignore(80,'\n');
          cout &lt;&lt; &quot;Genre: &quot;;
          cin.get(new_rec_ptr-&gt;genres,20);
      
          add_node(char game[50], char genre[20]);
      }
      
      }
      void search() //search though the program
      {
          char game[50];
          char genre[20];
          int counter;
          cout &lt;&lt;&quot; what do you want to search by? (1 for game and 2 for genre) &quot;;
          cin &gt;&gt; counter;
          cin.ignore(90,'\n');
      
          switch(counter)//chooses the 1 that the user has choosen
          {
      
              case 1://case 1
              {
                  current_ptr = head_ptr;
                  cout&lt;&lt; &quot;what game are you looking for? &quot;;
                  cin.get(game,50);
                  cin.ignore(90,'\n');
      
          while((strcmp(current_ptr-&gt;game_title, game)!= 0) &amp;&amp; (current_ptr != NULL))
          {
              current_ptr =current_ptr-&gt;next;
          }
          if(current_ptr != NULL)
          {
              cout &lt;&lt; current_ptr-&gt;game_title &lt;&lt; &quot;------&quot;;
              cout &lt;&lt; current_ptr-&gt;genres &lt;&lt; '\n';
          }
          break;
              }
          case 2://case2
              {
                  current_ptr = head_ptr;
                  cout&lt;&lt; &quot;what genre are you looking for? &quot;;
                  cin.get(genre, 20);
                  cin.ignore(90,'\n');
      
          while((strcmp(current_ptr-&gt;genres, genre)!= 0) &amp;&amp; (current_ptr != NULL))
          {
              current_ptr =current_ptr-&gt;next;
          }
          if(current_ptr != NULL)
          {
              cout &lt;&lt; current_ptr-&gt;game_title &lt;&lt; &quot;------&quot;;
              cout &lt;&lt; current_ptr-&gt;genres &lt;&lt; '\n';
          }
          break;
      
              }
          }
      }
      
      void find()
      {
          char search[50];
          previous_ptr= NULL;
          current_ptr = head_ptr;
          cout &lt;&lt;&quot;\nEnter the name of the game that you wish to delete: &quot;;
          cin.get(search,50);
          cin.ignore(80,'\n');
      
          while((strcmp(current_ptr-&gt;game_title, search)!= 0) &amp;&amp; (current_ptr != NULL))
          {
              previous_ptr = current_ptr;
              current_ptr= current_ptr-&gt;next;
          }
          if(current_ptr != NULL)
          {
              cout&lt;&lt; &quot;\ngame stuff\n&quot;;
              cout &lt;&lt; current_ptr-&gt;game_title &lt;&lt; &quot;    &quot;;
              cout &lt;&lt; current_ptr-&gt;genres &lt;&lt; endl;
              if(ready())
              {
                  delete_node(previous_ptr);
                  cout &lt;&lt; &quot;\nDELETED\n&quot;;
              }
              else
              {
                  cout &lt;&lt; &quot;\nits still here\n&quot;;
              }
          }
          else
          {
              cout &lt;&lt; &quot;\nThere wasn't a match.\n&quot;;
          }
      }
      int ready()
      {
          char answerYN;
          cout&lt;&lt;&quot;do you wish to to delete this?? (Y/N)\n&quot;;
          cin &gt;&gt; answerYN;
          if((answerYN == 'Y') || (answerYN == 'y'))
          {
              system(&quot;1&quot;);
          }
          else
          {
              system(&quot;0&quot;);
          }
      }
      
      void delete_node(Genres *previous_ptr)
      {
          if(current_ptr == head_ptr)
          {
              delete_head();
          }
          else
          {
              if(current_ptr-&gt;next == NULL)
              {
                  delete_end();
              }
              else
              {
                  delete_mid();
              }
          }
      }
      int handle(int number)
      
      {
          switch(number)
          {
          case 0:
              {
                  write_list_to_file();
                  if(head_ptr != NULL)
                  {
                      delete_L();
                  }
                  break;
              }
          case 1:
          {
                  find();             //displays
                  break;
              }
          case 2:
              {
                  search();                       //search
                  break;
              }
          case 3:
              {
              get_info(genre ,game);
      
              }
                  break;
      
          case 4:
              {
                  display();
                  break;
              }
          default :
              {
                  cout &lt;&lt; &quot;not a correct number\n&quot;;
                  break;
              }
          }
          system(&quot;pause&quot;);
      }
      void  delete_head()
      {
          current_ptr = head_ptr;
          if( head_ptr-&gt; next!= NULL)
          { 
              head_ptr = current_ptr-&gt;next;
          }
          else 
          {
              head_ptr= NULL;
          }
      delete current_ptr;
      }
      
      void delete_end()
      {
          delete current_ptr;
          previous_ptr-&gt;next = NULL;
          current_ptr = head_ptr;
      }
      void delete_mid()
      {
          previous_ptr-&gt;next = current_ptr-&gt;next;
          delete current_ptr;
          current_ptr = head_ptr;
      }
      

      sorry for being a punk about all this im just been working on it for school...

       
    • cpns

      cpns - 2009-03-07

      An "access violation" is a run-time error. Given that this code will not compile, this cannot be the code you are running.

      Line 248:

      > add_node(char game[50], char genre[20]);

      That is not a valid function call. If I change that to:

      > add_node(game, genre);

      I then get:

      sandbox.cpp(392) : warning C4100: 'previous_ptr' : unreferenced formal parameter
      sandbox.cpp(233) : error C4716: 'pos_insert_point' : must return a value
      sandbox.cpp(390) : error C4716: 'ready' : must return a value
      sandbox.cpp(453) : error C4716: 'handle' : must return a value

      Although these are VC++ messages, I have no reason to believe that it is any more compilable in GCC.

      The thing is we could wade through all that code to try to find the error, but that is unlikely to be productive, in that amount of code it is likely that we'll find a number of flaws, which may or may not be related to your problem. So the obvious solution is to compile it, run it , reproduce your error, and debug it. However this is not the same code as that which you are running, so even that might be pointless, even if we could compile it. And the code is interactive and has a number of possible execution paths, unless you also provide a test case with specific steps to reproduce the error, you are still making unnecessarily hard. It also required a data file, so and example or definition of its content and format would be necessary.

      Of course you could also use the debugger to investigate the problem yourself. which is exactly what I would do if I could. If you choose to use the abysmal Dev-C++ debugger, good luck.

      Clifford

       
    • Wayne Keen

      Wayne Keen - 2009-03-08

      Interestingly, here is what I get when I compile it OOB with GCC-4.3.2 under MinGW

      g++ testit66.cpp
      testit66.cpp: In function 'void get_info(char, char)':
      testit66.cpp:284: error: expected primary-expression before 'char'
      testit66.cpp:284: error: expected primary-expression before 'char'

      which is the same error to which you referred Clifford.

      When I fix it as you did, it does compile.

      Interesting

       
    • Donnie Hammond

      Donnie Hammond - 2009-03-08

      well i went to school and i got the code to do what i want it to do but at home it wont work but thank you for both your advices, it was a high schoolers mistake. take a look at get_info its suppose to be a int get_info not a void and with some changing of that and it would work as a link list like i wanted it to... also the add_node i had figured out right after i posted it so i was feeling like an idiot after i posted it.. at least thats what i did at my school but all in all the only thing is i just didnt know what the "An Access Violation (Segmentation Fault) raised in your program" was talking about and i didnt know what to do

       
      • cpns

        cpns - 2009-03-08

        > i just didnt know what the "An Access Violation (Segmentation Fault) raised in your program" was talking about and i didnt know what to do

        Typically when you run code in a debugger, and an access violation occurs, it will break at the point in the code the violation occurred, and you can inspect the variables and stack etc.

        What it means is that there was an attempt to access a memory location not within your process's control. Usually a bad pointer or a buffer overrun. Unfortunately the point at which teh program crashes and the root cause need not be in the same location, so you need to be a bit forensic or just lucky in your debug activity.

        Another problem you may encounter is the code runs in one environment and not in another, since the behaviour of such bugs is non-deterministic. This means for example that code that breaks when run directly works in the debugger and vice-versa, or it works on one machine and not another. I mention this because you may find that your 'working' code is in fact still flawed, since you say it works at school, but not at home.

        Careful coding practices can help. For linked lists you have to have a really good reason not to use the standard <list> class template.

        Clifford

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.