Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Adding words to a txt file. need help! please

Posted on 2007-12-01
6
Medium Priority
?
266 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 1600 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

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!

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

722 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