Random line read
Post your questions regarding programming in C++ in here.
5 posts
Page 1 of 1
Hello
I want to solve it randomly reads the lines.
I found a source code, but it will crash the program.
This is source code:
I want to solve it randomly reads the lines.
I found a source code, but it will crash the program.
This is source code:
Code: Select all
Txt file content:
DWORD cFindEvent::GetRandomPosition()
{
//---------------------------------------------------------------------------------------
FILE * garew = fopen (MobCoords,"r");
//---------------------------------------------------------------------------------------
char temp[1024];
int i = 0;
int j = 0;
int licz = rand() % 100;
//---------------------------------------------------------------------------------------
while ( !feof(garew) && i <= licz)
{
//-----------------------------------------------------------------------------------
fgets(temp , 1024 , garew );
//-----------------------------------------------------------------------------------
if(temp[0] != '/' && temp[1] != '/')
{
//-------------------------------------------------------------------------------
sscanf(temp,"%d %d %d", &DayMap, &DayX, &DayY);
if(DayMap >= 0)
{
//---------------------------------------------------------------------------
i++;
//---------------------------------------------------------------------------
}
//-------------------------------------------------------------------------------
}
//-----------------------------------------------------------------------------------
}
//---------------------------------------------------------------------------------------
fclose(garew);
//---------------------------------------------------------------------------------------
FILE * garew2 = fopen (MobCoords,"r");
//---------------------------------------------------------------------------------------
if(licz > i)
{
//-----------------------------------------------------------------------------------
int licz2 = rand() % i;
//-----------------------------------------------------------------------------------
while ( j <= licz2 )
{
//-------------------------------------------------------------------------------
fgets(temp , 1024 , garew2 );
//-------------------------------------------------------------------------------
if(temp[0] != '/' && temp[1] != '/')
{
//---------------------------------------------------------------------------
sscanf(temp,"%d %d %d", &DayMap, &DayX, &DayY);
if(DayMap >= 0)
{
//-----------------------------------------------------------------------
j++;
//-----------------------------------------------------------------------
}
//---------------------------------------------------------------------------
}
//-------------------------------------------------------------------------------
}
//-----------------------------------------------------------------------------------
}
//---------------------------------------------------------------------------------------
fclose(garew2);
//---------------------------------------------------------------------------------------
DWORD Reward = DNState.DayBossIndex=CreateMobInXY(21,DayMap,DayX,DayY);
Chat.MessageAll(0,0,NULL,"The monster entered the coordinates: %d x %d", DayX, DayY);;
//---------------------------------------------------------------------------------------
return Reward;
//---------------------------------------------------------------------------------------
}
Code: Select all
What's wrong with this?0 63 120
0 63 125
0 70 140
I don't know much about C++, but it seems to me the error might be in your fopen() function:
I'm assuming MobCoords is a variable? Where in your code is that variable located... if it doesn't exist, you may have your problem right there lol
Code: Select all
According to the C++ reference, the first parameter of fopen() has to be a Char variable, such as "filename.txt".fopen (MobCoords,"r")
I'm assuming MobCoords is a variable? Where in your code is that variable located... if it doesn't exist, you may have your problem right there lol
comathi wrote:I don't know much about C++, but it seems to me the error might be in your fopen() function:This has included:
Code: Select allAccording to the C++ reference, the first parameter of fopen() has to be a Char variable, such as "filename.txt".fopen (MobCoords,"r")
I'm assuming MobCoords is a variable? Where in your code is that variable located... if it doesn't exist, you may have your problem right there lol
Code: Select all
#define MobCoords "..\\Config\DayCoords.txt"
int DayMap;
int DayX;
int DayY;
int DayBossIndex;
The filename looks wrong.
If you're trying to access a file in the parent (previous) folder, it should be
If you're trying to access a file in the parent (previous) folder, it should be
Code: Select all
I think you've got one too many backslashes at the beginning ;)"..\Config\DayCoords.txt"
Here's a piece of code written in C++ which might help you.
Code: Select all
A few tips:
#include <iostream>
#include <string>
#include <fstream>
#include <time.h>
int main()
{
const std::string fileName = "path/to/file.extension";
// first, never forget to seed the number generator
// if you didn't do it already
srand(time(NULL));
// open the file
std::ifstream file(fileName);
if (file.is_open())
{
// lets count the total number of lines
int linesCount = 0;
std::string line;
while (std::getline(file, line))
{
++linesCount;
}
// reopen the file
file.close();
file.open(fileName);
// pick a random line
// tip: don't use rand(), check on the internet for
// randomness in C++11
const int randomLine = rand() % linesCount;
// read lines until we reach our desired random line
for (int crtLine = 0; crtLine <= randomLine; ++crtLine)
{
std::getline(file, line);
}
// use the line here
int iDayMap = 0;
int iDayX = 0;
int iDayY = 0;
sscanf(line.c_str(), "%d %d %d", &iDayMap, &iDayX, &iDayY);
// line is our std::string object
// line.c_str() returns a const char* string
// which can be used with sscanf
// another tip: if available, use sscanf_s, which should be more secure
// than sscanf, but if that is no concern, you can use sscanf
// sscanf_s(line.c_str(), "%d %d %d", &iDayMap, &iDayX, &iDayY);
}
}
- As I said in the comments of the code, you should not use srand() and rand(). I did it just to make it simple.
- If you plan in getting random lines a lot of times, do not open and read the file everytime. Consider reading once, add them in a vector then everytime you need a random line, get it from the vector. You have everything there, including the lines count (the size of vector). Even better: made a struct with the 3 variables (iDayMap, iDayX, iDayY), and add those in the vector. This way, you won't have to do sscanf everytime. It will be faster.
- Avoid defines (except include guards)
- Try to define paths using / (forward slash) instead of \ (back slash) because of character escaping. It is easier, and works on every platform.
If somebody helps you, don't forget to say "Thank You!".
5 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023