Solved

Adding words to a txt file. need help! please

Posted on 2007-12-01
6
263 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
Independent Software Vendors: 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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

738 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