Solved

Adding words to a txt file. need help! please

Posted on 2007-12-01
6
262 Views
Last Modified: 2013-12-14
Ok I have been working on this code for awhile. So far I set the file to be loaded, I can show a list of the words in the file, but when I choose to add a word it wont save it to the file. I also get this warning not an error:

.\game.cpp(79) : warning C4996: 'strcpy' was declared deprecated
        C:\Program Files\Microsoft Visual Studio 8\VC\include\string.h(73) : see declaration of 'strcpy'
        Message: 'This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.'

Here is my code in game.cpp:

#include <iostream>
#include <string>
#include <fstream>
#include "game.h"

#define MAX_WORD_SIZE 15
#define MAX_WORDS 255

typedef char String[MAX_WORD_SIZE];
String Words[MAX_WORDS - 1];
int Count;
char Word[MAX_WORD_SIZE];
int Size;
int Loop;

using namespace std;

void Game::loadFile()
{
      char C;
      ifstream Datfile;

      Count = 0;

      Datfile.open("words.txt");

      while((C=Datfile.peek()) !=EOF)
      {
            Datfile >> Words[Count++];

            if(Count > MAX_WORDS - 1)
            {
                  cout << "\nToo many words in the file, stopping with " << MAX_WORDS << " words." << endl;
                  Count = MAX_WORDS;
                  break;
            }
      }

      Count--;

      Datfile.close();
}

void Game::listWords()
{
      Game myGame;
      myGame.loadFile();

      cout << "\tThe following words in the file are: \n" << endl;

      for(int Loop=0; Loop < Count - 1; Loop++)
      {
            cout << "\t" << Words[Loop] << "\n";
            if (++Loop > Count)
                  break;
            cout << "\t" << Words[Loop] << "\n";
            if (++Loop > Count)
                  break;
            cout << "\t" << Words[Loop] << "\n";
            if (++Loop > Count)
                  break;
            cout << "\t" << Words[Loop] << "\n";
            if (++Loop > Count)
                  break;
            cout << "\t" << Words[Loop] << "\n";
      }
}
void Game::addWords()
{
      Game myGame;
      myGame.loadFile();
      ofstream Datfile;

      cout << "\tEnter a new word, keep it below " << MAX_WORD_SIZE << " characters: ";
      cin >> Word;

      Size = strlen(Word);

      strcpy(Words[Count++],Word);

}
0
Comment
Question by:jschmuff
  • 5
6 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 20389085
Just add "_CRT_SECURE_NO_DEPRECATE" to your project settings under "C++|Preprocessor|Additional symbols".
0
 
LVL 86

Expert Comment

by:jkr
ID: 20389092
Or use

      strcpy_s(Words[Count++],MAX_WORD_SIZE,Word);

instead.
0
 
LVL 86

Expert Comment

by:jkr
ID: 20389094
To elaborate: MS added this as an extension to ANSI C/C++, since a lot of security holes were caused by buffer overruns that were exploitet maliciously. Note that your code won't be portable unless you set _CRT_SECURE_NO_DEPRECATE.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jschmuff
ID: 20389192
ok I added

   strcpy_s(Words[Count++],MAX_WORD_SIZE,Word);

still not adding the word to the file.
0
 
LVL 86

Accepted Solution

by:
jkr earned 400 total points
ID: 20389221
Seems like you forgot to output it.

You should

void Game::addWords()
{
      Game myGame;
      myGame.loadFile();
      ofstream Datfile;

      cout << "\tEnter a new word, keep it below " << MAX_WORD_SIZE << " characters: ";
      cin >> Word;

      Size = strlen(Word);

      strcpy(Words[Count++],Word);

      for (int i = 0; i < Count; ++i) Datfile << Words[i] << endl;

}
0
 
LVL 86

Expert Comment

by:jkr
ID: 20389223
Sorry, that should be

ofstream Datfile("Words.txt");

in the above.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question