Solved

Adding words to a txt file. need help! please

Posted on 2007-12-01
6
264 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: 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!

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

624 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