Solved

Adding words to a txt file. need help! please

Posted on 2007-12-01
6
257 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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.

762 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now