Producing a random file name, and then creating the file, In C++ - c++

In my program, I'm attempting to Generate a random file name, and then use fopen to create a file with that name. The process goes as following
Create a random file name
Check if we are administrator by attempting to create a file with that name in c:\
Write stuff to the file
The function I use to make the random file name is:
const char *RandomName(const char *suffix,unsigned int length)
{
const char alphanum[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
int stringLength = sizeof(alphanum) - 1;
std::string Str;
unsigned int i;
Str.append("c:\\");
for( i = 0; i < length; ++i)
{
Str += alphanum[rand() % stringLength];
}
Str += suffix;
const char *str =Str.c_str();
return str;
}
The function I use to create the file, and check for Admin is:
bool IsAdmin()
{
const char *n = RandomName(".BIN",5);
cout << n << endl;
FILE *fp;
fp = fopen((const char *)n,"w+");
if (fp == NULL) {
cout << "File pointer was NULL" << endl;
return false;
} else {
cout << "File pointer is legit" << endl;
//fclose(fp);
//remove(n);
int b;
for(b = 0; b != 1338; b++)
{
char f = 'c';
fputc(f, fp);
}
return true;
}
}
When run as Admin, The program prints:
c:\9UswA.BIN
Not Admin!
How do I get the program to create a file with a name that matches what it shows on screen? and without sketchy behavior?

Simple
just use tmpnam c api
example:
#include <stdio.h>
int main(void)
{
char name[40];
int i;
for(i=0; i<3; i++) {
tmpnam(name);
printf("%s ", name);
}
return 0;
}

I think running the .exe created here as administrator would let you create the file.
Run the program not from any IDE but manually from file location and run as admin.

To solve the problem, I got rid of the RandomName function and edited IsAdmin to include its code, with a few tweaks I was able to get it to work at least somewhat well, the code I ended up with was:
void AdminWrite(const char *suffix,unsigned int length)
{
FILE *fp;
const char alphanum[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv";
int stringLength = sizeof(alphanum) - 1;
std::string Str;
unsigned int i;
Str.append("c:\\Users\\UserName\\Desktop\\");
for( i = 0; i < length; ++i)
{
Str += alphanum[rand() % stringLength];
}
Str += suffix;
const char *str =Str.c_str();
cout << str << endl;
fp = fopen(str,"w+");
if (fp == NULL) {
cout << "File pointer was NULL" << endl;
return;
} else {
cout << "File pointer is legit" << endl;
int b;
for(b = 0; b != 1337; b++)
{
char f = 'c';
fputc(f, fp);
}
return;
}
return;
}

Related

Trying to concatenate array of c-strings with delimiter

Here is my code
int main(int argc, char *argv[]) {
char const *strings[10] = {"dhh", "aci", "cdh"};
join_def(strings, 'l');
return EXIT_SUCCESS;
}
// part 1 read lines
void join_def(char const **strings, char delim) {
char *t = new char[100];
//int length = 0;
t[0] = '\0';
int x = sizeof(strings);
std::cout << delim << std::endl;
for (int i = 0; i < x; i++) {
int size = 0;
while(strings[i][size]!='\0'){
size++;
std::cout << strings[i][size] << std::endl;
}
}
}
I have spent hours now I just can't get to concatenate it
For this task, I cannot use cstring or anything other than iostream so please don't suggest.
The output needs to be a c-string = "dhhlacilcdh"
First, you cannot determine the number of elements in an array passed to a function, as that array will decay to a simple pointer. So, your sizeof(strings) expression will evaluate (at compile time) to the (fixed) size, in bytes, of a pointer. For the function to be 'aware' of how many elements there are in an array, it needs to be explicitly told (by way of an extra argument).
Second, you have your i' and size indexes the wrong way round in the std::cout << strings[i][size] << std::endl; line and, further, you increment size before printing the relevant character, whereas it should be incremented after you've printed it.
The code below also does the actual concatenation of the strings, and the modified join_def function now returns a pointer to that result (which must be freed when you're finished with it);
#include <iostream>
char* join_def(char const** strings, char delim, int x)
{
char* t = new char[100];
int length = 0;
t[0] = '\0';
//int x = sizeof(strings);
std::cout << delim << std::endl;
for (int i = 0; i < x; i++) {
int size = 0;
while (strings[i][size] != '\0') {
std::cout << strings[i][size] << std::endl;
t[length++] = strings[i][size]; // Append this character
size++;
}
t[length++] = delim; // Append delimiter
}
t[length] = '\0'; // Append nul-terminator
return t;
}
int main()
{
char const* strings[10] = { "dhh", "aci", "cdh" };
char* result = join_def(strings, 'l', 3);
std::cout << result << std::endl;
free(result);
return 0;
}
Note, also, that I have moved the join_def function code to before the main (which calls it). If you don't do this, then will at least have to provide a (forward) declaration of that function before main (just a char* join_def(char const** strings, char delim, int x); on its own will do).
Feel free to ask for further clarification and/or explanation.
I'm not exactly sure what you're trying to do, but maybe this helps?
#include <iostream>
// part 1 read lines
void join_def(char const **strings, char delim)
{
char *t = new char[100];
//int length = 0;
t[0] = '\0';
int x = 0;
for (int i = 0; strings[i] != nullptr; i++)
x += sizeof(strings[i]) - 1;
std::cout << delim << std::endl;
for (int i = 0; strings[i] != nullptr; i++)
{
int size = 0;
while (strings[i][size] != '\0')
{
size++;
std::cout << strings[i][size] << std::endl;
}
}
}
int main(int argc, char *argv[])
{
char const *strings[] = {"dhh", "aci", "cdh", nullptr};
join_def(strings, 'l');
return EXIT_SUCCESS;
}
this is what you are looking for?
look that I remove all the std::endl because it like '\n'
also i moved your size++ after the std::cout
#include <iostream>
// part 1 read lines
void join_def(char const **strings, char delim,int length) {
char *t = new char[100];
//int length = 0;
t[0] = '\0';
int x = length;
for (int i = 0; i < x; i++) {
int size = 0;
while(strings[i][size]!='\0'){
std::cout << strings[i][size]; //<--print befure increment size
size++;
}
std::cout << delim;
}
}
int main(int argc, char *argv[]) {
char const *strings[] = {"dhh", "aci", "cdh"};
join_def(strings,'|',3); //<- need to send the length of the char* array
return EXIT_SUCCESS;
}

auto, template? How to construct method returning different datatypes

[INFO]
I'm new here and... new to programming. I'm learning C (with very little of C++) for about a year and now I'm stuck. I'm currently writing my first bigger application for training purposes and as every programist - I'm trying to be as lazy as I could. My application is based on Simple Fast Multimedia Library (SFML) for drawing graphics and of course C/C++ for logic. I was tired of having lots of different variables hidden in code (like Window Resolution, Window Position etc.) soo I started writing class for reading *txt files with configuration (You know: config.ini with something like "iWindowResolutionX=1920;"). Algorythm that opens *txt files, interprets them and pulls out needed data is working (proppably it is very bad - but hey, it is working :P)
[QUESTION]
I'm stuck with very basic thing.
I have object of my config file reader class and I want to achieve something like this:
Int main()
{
int WindowResolutionX = 0; //nothing special - int with value of 0.
CfgReader cfg_object("config.ini"); // object of CfgReader class with custom
constructor passing name of *txt
configuration file.
WindowResolutionX = cfg_object.Search("iWindowResolutionX"); // As you see - I want to write a code that calls method "Search" on cfg_object. This method takes variable name as parameter and after sucsess (value found) returns it.
And here I am stuck. How to force method to return different basic data types( char, int, float etc. )?
}
I've tried defining method return type as "auto", but it gives me an error "a function that returns 'auto' cannot be used before it is defined" (I don't understand what's VS is talking to me here) and then errors about trying to return different datatypes than firstly selected (this is simple, if compiler see "return value" for the first time, it can't allow me to return any other datatype).
Next thing I've tried was template methods, but I'm to stupid to understand it ;)
Ok, there is simple solution - I can copy my Search method X times for X datatypes I want and just simply overload it - but this is not elegant solution and won't teach me anything new. Below my code:
"CfgReader.h"
#pragma once
#include "fstream"
#include <iostream>
#include <string>
class CfgReader
{
public:
FILE *fp;
const char * cfg_filename;
size_t filesize;
std::string line;
int int_val;
float float_val;
char char_val;
bool bool_val;
long long_val;
std::string string_val;
size_t size_t_val;
public:
void OpenFile(const char * filename);
void CloseFile();
auto Search(const char * search_val);
void Show_content();
int r_int();
char r_char();
float r_float();
size_t r_size_t();
long r_long();
bool r_bool();
std::string r_string();
CfgReader();
CfgReader(const char *);
~CfgReader();
};
"CfgReader.cpp"
#include "CfgReader.h"
#pragma warning(disable: 4996)
CfgReader::CfgReader()
{
CfgReader("");
}
CfgReader::CfgReader(const char * filename)
{
if ((sizeof(filename) == 1) && (filename[0] == 0))
{
std::cout << "\n CfgReader No filename.";
cfg_filename = "";
fp = NULL;
}
else
{
std::cout << "\n test";
line = "";
int_val = NULL;
float_val = NULL;
char_val = NULL;
bool_val = false;
long_val = NULL;
string_val = "";
size_t_val = NULL;
cfg_filename = filename;
OpenFile(cfg_filename);
}
}
void CfgReader::OpenFile(const char * filename)
{
fp = fopen(filename, "rb");
if (fp != NULL)
{
std::cout << "\n good!";
}
else
{
std::cout << "\n Error, could not open file.";
}
rewind(fp);
fseek(fp, 0, SEEK_END);
filesize = ftell(fp);
rewind(fp);
std::cout << "\n filesize: " << filesize;
//system("pause");
}
void CfgReader::Search(const char * search_val)
{
size_t search_val_length = 0;
for (search_val_length; search_val[search_val_length] != '\0';
search_val_length++);
std::string test;
if (fp == NULL)
{
std::cout << "\n Error, file not loaded!";
}
else
{
char first_letter = 0;
bool match = false;
size_t counter = 0;
rewind(fp);
while (match == false)
{
while (first_letter != 13)
{
fread(&first_letter, sizeof(char), 1, fp);
}
if (first_letter == 13)
{
fread(&first_letter, sizeof(char), 1, fp);
if (first_letter == 10)
{
do
{
fread(&first_letter, sizeof(char), 1, fp);
if (first_letter == search_val[counter])
{
test += first_letter;
counter++;
if(counter==search_val_length)
{
match = true;
break;
}
}
else
{
counter = 0;
test = "";
break;
}
} while (first_letter != 61);
if (test == search_val || match == true)
{
match = true;
break;
}
}
}
std::cout << "\n ftell(fp): " << ftell(fp);
if (ftell(fp) == filesize) break;
}
if (match == false)
{
std::cout << "\n ERROR, no such VALUE!";
}
else
{
std::cout << "\n test string = " << test;
//system("pause");
//Show_content();
///line = test;
///test = "";
while (first_letter != 13)
{
fread(&first_letter, sizeof(char), 1, fp);
if (first_letter == 61)continue;
test += first_letter;
}
std::cout << "\n test string VALUE (string):" << test << std::endl;
switch (line[0])
{
case 'i':
int_val = std::stoi(test);
std::cout << "\n int_val: " << int_val;
//return int_val;
//a = int_val;
break;
case 'f':
float_val = std::stof(test);
std::cout << "\n float_val: " << float_val;
//return float_val;
//a = float_val;
break;
case 'b':
if (test[0] == 'f' || test[0] == '0') bool_val = false;
else bool_val = true;
std::cout << "\n bool_val: " << bool_val;
//return bool_val;
//a = bool_val;
break;
case 'l':
long_val = std::stol(test);
std::cout << "\n long_val: " << long_val;
//return long_val;
//a = long_val;
break;
case 's':
string_val = test;
std::cout << "\n string_val: " << string_val;
//return string_val;
// a = string_val;
break;
case 't':
size_t_val = std::stoul(test);
std::cout << "\n size_t_val: " << size_t_val;
//return size_t_val;
//a = size_t_val;
break;
}
}
}
}
int CfgReader::r_int()
{
return int_val;
}
char CfgReader::r_char()
{
return char_val;
}
float CfgReader::r_float()
{
return float_val;
}
size_t CfgReader::r_size_t()
{
return size_t_val;
}
long CfgReader::r_long()
{
return long_val;
}
bool CfgReader::r_bool()
{
return bool_val;
}
std::string CfgReader::r_string()
{
return string_val;
}
void CfgReader::Show_content()
{
std::cout << "\n //--------------------------CfgReader.Show_content()------------------------\\"<<std::endl;
if (fp != NULL)
{
rewind(fp);
fseek(fp, 0, SEEK_END);
int filesize = ftell(fp);
char literka;
rewind(fp);
while (ftell(fp) != filesize)
{
fread(&literka, sizeof(char), 1, fp);
std::cout << "\n" << (short)literka << " - " << literka;
}
}
else
{
std::cout << "\n Error: fp == NULL.";
}
std::cout << "\n \\--------------------------/CfgReader.Show_content()------------------------// \n";
}
void CfgReader::CloseFile()
{
fclose(fp);
}
CfgReader::~CfgReader()
{
}
"Source.cpp"
#pragma warning(disable:4996)
#include "CfgReader.h"
int main()
{
CfgReader Config("config.ini");
Config.Search("iBitDepth");
system("pause");
return 0;
}
"config.ini"
//------------------------ M_UPTIME config FILE ------------------------//
[Window]
iWindowResX=1920
iWindowResY=1080
fWindowScale=1
iWindowPosX=-1920
iWindowPosY=0
iBitDepth=32
[Screen Saver]
iScreenSaverTimeLimit=600
[TestValues]
bThisIsBool=false
bThisIsBoolToo=true
sThisIsString=Ala ma kota a kot ma ale
Anyone can point me out how to convert Search method for being able to either return different datatypes:
int main()
{
int value_1 = 0;
CfgReader Config("config.ini");
value_1 = Config.Search("iBitDepth");
return 0;
}
or work as here: (CfgReader object gets reference to variable as a parameter)
int main()
{
int value_1 = 0;
CfgReader Config("config.ini");
Config.Search("iBitDepth", &value_1);
return 0;
}
If someone could also give me a example on how to properly convert my Search method for template method.
Thanks for responces, I'm out of ideas...
I'd suggest to implement separate methods for each data type.
You know data type of the target variable, right? Then you can do few methods like this:
int GetInt(const char* name, int defaultValue = 0) const;
std::string GetString(const char* name, const char* defaultValue = "") const;
And call appropriate method:
int bitDepth = config.GetInt("Window.iBitDepth", 24);
One way to do this using templates is to use "lexical cast" (as Boost calls it). Implementing simple lexical cast function using std::istringstream is relatively simple:
template<typename TargetType>
TargetType lexical_cast(const std::string& source)
{
TargetType result;
// create a stream for reading, initially containing source string
std::istringstream stream(source);
// read one value of type TargetType...
stream >> result;
// ...and return it
return result;
}
This will work for all types for which operator >> is overloaded, which includes primitive types, such as float or int. Additionally, you can overload the operator for your custom types.
With this, you can implement Search template method which converts a string to requested type:
template<typename TargetType>
TargetType Search(const std::string& key) const
{
std::string valueAsStr = // get it somehow, from a map, or file...
return lexical_cast<TargetType>(valueAsStr);
}
I've tried defining method return type as "auto", but it gives me an error "a function that returns 'auto' cannot be used before it is defined" (I don't understand what's VS is talking to me here)
Personally, I don't use VS too often, but I suppose this means that compiler must see body (definition) of the method when you call it. This is necessary - after all, when using auto (without trailing return type), compiler has to deduce returned type based on body of the function.

Reading input not knowing its size in c++

I have not found good answer for my question so i decided to ask my own question.
I need to read input from console with my own function in following format:
a RL
b RLRL
c RLLL
...
in general:
(character)(space)(character sequence or empty)(new line character)
I want to read it into char arrays one by one. I mean I want to read one line, do something with data, then read second one and so on...
Problem is that I do not know number of lines and I do not know how to stop it.
Checking if it is new line character (ASCII 10) does not work because it is in every single line.
There is my code:
#include <iostream>
#include <cstdio>
using namespace std;
int input_size;
char c;
bool finish;
inline int read(char* v, char* arr) {
int counter = 0;
c = getchar();
*v = c; //Pobranie wartości węzła
input_size++;
char* tmp = arr;
c = getchar();
c = getchar();
while (c != 32 && c != 10) {
input_size++;
*tmp++ = c;
c = getchar();
counter++;
}
if(c == 10)
finish = false;
return counter;
}
void show(char* t, int c) {
char* tmp = t;
for(int i = 0; i < c; i++) {
cout << *tmp;
tmp++;
}
delete[] tmp;
}
int main()
{
finish = true;
char* a = new char[1];
char* b = new char[64];
while(finish) {
int c = read(a, b);
cout << "char: " << *a << endl;
cout << "char sequence: ";
show(b,c);
cout << endl;
}
return 0;
}

Program crashes due to undefined behavior

So I'm writing a program that creates a library for a collection of CDs and displays them. My program compiles but crashes whenever I write an array of pointers to songs from a file into structs contained within an array shown here:
//Get song array
for (int a = 0; a < num_songs; a++)
{
getline (infile, line);
sub = line.c_str();
word = createString(sub);
length = substr(word, -1, 5);
title = substr(word, 5, strlen(sub));
cd->song_array[a] = createSong(title,length);
destroyString(word);
}
I think it's due to undefined behavior, here's the .cpp file that this is happening in.
#include <iostream>
#include "CDs.h"
#include "CD.h"
#include <fstream>
#include <string>
#include <cstring>
using namespace std;
//Creates a collection of CDs
CDs* createCDs(const char* file_name)
{
//Declare variables and allocate memory
int max_cds = 50;
CDs* collection = new CDs;
collection->max_cds = max_cds;
CD** cd_array = new CD*[max_cds];
int num;
int sentinel = 0;
String* word;
string line;
CD* cd;
const char* sub;
String* length;
String* title;
//Open .txt file
ifstream infile;
infile.open(file_name);
if (infile.is_open())
{
while (infile.good())
{
for (int i = 0; i < max_cds; i++)
{
//Get the artist from .txt file
cd = cd_array[i];
getline (infile, line);
sub = line.c_str();
word = createString(sub); //Create string from infile line
cd->artist = word;
destroyString(word);
//Get the Title of the album from file
getline (infile, line);
sub = line.c_str();
word = createString(sub);
cd->title = word;
destroyString(word);
//Get the Year of the album from file
infile >> num;
cd->year = num;
//Get the Rating
infile >> num;
cd->rating = num;
//Get number of tracks
int num_songs;
infile >> cd->num_tracks;
//Get song array
for (int a = 0; a < num_songs; a++)
{
getline (infile, line);
sub = line.c_str();
word = createString(sub);
cout << "SHIT" << endl;
length = substr(word, -1, 5);
title = substr(word, 5, strlen(sub));
cd->song_array[a] = createSong(title,length);
destroyString(word);
}
cd_array[i] = cd;
sentinel++;
}
}
}
else
{
cout << "file did not open";
}
collection->cd_array = cd_array;
collection->num_cds = sentinel;
collection->max_cds = max_cds;
return collection;
}
I have no idea what to do to make this run, If someone could help that would be amazing.
edit - I didn't give the .cpp that is included and has some of the functions used
#include <iostream>
#include <cstring>
#include "String.h"
using namespace std;
//Function that creates a string
String* createString(const char* char_array)
{
//Allocate memory for a pointer to String struct
//String* string;
String* string = new String;
//Write the char_array to String struct
int length = strlen(char_array);
char array[30];
for (int i = 0; i <= length; i++)
{
array[i] = char_array[i];
string->array[i] = array[i];
}
return string;
}
//Function that displays the string
void displayString(String* str)
{
for (int i = 0; i < strlen(str->array); i++)
{
cout << str->array[i];
}
cout << endl;
}
//Function that destroys the string
void destroyString(String* str)
{
delete str;
str = NULL;
}
int find(String* str, char delimiter, int start)
{
for (int i = start; i <= strlen(str->array); i++)
{
if (str->array[i] == delimiter)
{
return i;
}
}
cout << "No occurences of delimiter were found" << endl;
return -1;
}
String* substr(String* str, int start, int end)
{
String* new_str = new String;
int count = 0;
for (int i = start + 1; i < end - 1; i++)
{
new_str->array[count] = str->array[i];
count++;
}
return new_str;
}
void compare(String* str1, String* str2)
{
if (str1->array < str2->array)
{
cout << str1->array << " is less than " << str2->array << endl;
}
if (str1 > str2)
{
cout << str2->array <<" is less than " << str1->array << endl;
}
if (str1 == str2)
{
cout << "The strings are equal" << endl;
}
}
You never allocate memory for the effective CD. You just allocate an array of pointers to CDs (cd_array). This means you have an array of pointers, pointing to unkown memory locations.
The best way to ensure no such bad access is possible, is to not dynamically allocate memory. Just use CD cd_array[max_cds] and work with that. (Use call by reference, if you need to pass this to a function.)
You need to add the following lines:
//Get the artist from .txt file
cd_array[i] = new CD;
cd = cd_array[i];
.....
You should de-allocate the memory once finished using them
for(...)
delete cd_array[i];
delete []cd_array;

Working with structs?

I'm having trouble working with strings (char*) in structs. I can't seem to call the right data I want too.
Under processFile it shows the struct member correctly; under main it does not.
Here is my code:
#include <iostream>
#include <io.h>
#include <string>
#include "dirent.h"
#include "Stream.h"
#include "Compression.h"
#include "Definitions.h"
using namespace std;
using namespace System::IO;
using namespace System::Funcs;
bool isRawfile(char* ext);
void ProcessDirectory(string directory);
void ProcessFile(char* file);
void ProcessEntity(struct dirent* entity);
typedef struct
{
char *name;
int usize;
int csize;
BYTE *data;
} rawfile;
string path = "";
string source;
int numrawfiles = 0, numstringtables = 0;
rawfile *rawfiles = new rawfile[0x400];
FILE * zone = fopen( "C:\\Users\\jake\\Desktop\\patch_mp.zone" , "wb" );
int main(int argc, char **args)
{
if(args[1] != NULL)
{
source = string(args[1]) + "\\"; //maybe move under else here..
if(strchr(args[1], '.') != NULL)
{
cout<<"Unable to compile files, please drag a folder to compile."<<endl;
cin.get();
return 0;
}
else
{
int header[] = {1,0,0x3B4,0,0,0,1,0,0x1000,0,0,0,-1};
for(int i=0; i<13; i++)
fwrite(Converter::Int32ToBytes(header[i]), 1 , 4 , zone );
ProcessDirectory(args[1]);
for(int i=0; i<numrawfiles; i++)
cout<<"Name: "<<rawfiles[i].name<<" Length: "<< rawfiles[i].usize << " - in main()"<<endl;
fclose(zone);
}
}
else
{
cout<<"No folder selected to compile. Press any Key to quit."<<endl;
cin.get();
return 0;
}
cin.get();
return 0;
}
void ProcessDirectory(string directory)
{
string dirToOpen = path + directory;
auto dir = opendir(dirToOpen.c_str());
path = dirToOpen + "\\";
if(NULL == dir)
{
cout << "could not open directory: " << dirToOpen.c_str() << endl;
return;
}
auto entity = readdir(dir);
while(entity != NULL)
{
ProcessEntity(entity);
entity = readdir(dir);
}
path.resize(path.length() - 1 - directory.length());
closedir(dir);
}
void ProcessEntity(struct dirent* entity)
{
if(entity->d_type == DT_DIR)
{
if(entity->d_name[0] == '.')
return;
ProcessDirectory(string(entity->d_name));
return;
}
if(entity->d_type == DT_REG)
{
string fullpath = path + entity->d_name;
ProcessFile(const_cast<char *>(fullpath.c_str()));
return;
}
cout << "Not a file or directory: " << entity->d_name << endl;
}
void ProcessFile(char* file)
{
char* extension = strrchr(file, '.');
if(isRawfile(extension))
{
rawfile raw;
raw.name = (char *)&file[source.length()];
raw.usize = File::getFileSize(file);
rawfiles[numrawfiles] = raw;
cout<<"Name: "<<rawfiles[numrawfiles].name<<" Length: "<< raw.usize << " - in ProcessFile()"<<endl;
fwrite(Converter::Int32ToBytes(0x23),1,4,zone);
fwrite(Converter::Int32ToBytes(-1),1,4,zone);
numrawfiles++;
}
}
bool isRawfile(char* ext)
{
char *exts[11] = {".gsc",".cfg",".txt",".news",".png",".vision",".rmb",".script",".arena",".atr",".csc"};
for(int i=0; i<10; i++)
if(strncmp(ext,exts[i],strlen(exts[i]))==0)
return true;
return false;
}
Here is an example picture:
What am I doing wrong?
Save yourself a lot of trouble by using an std::string:
#include <string>
struct thing
{
std::string name;
int age;
};
You can also avoid the dynamically allocated array:
#include <vector>
std::vector<thing> things(3);
You are outputting the memory address of "Ben" instead of the actual String. You should use
cout << things[1]->name << endl;
which is syntactic sugar for
cout << (*things[1]).name << endl;
To add to what scd said, you are not successfully dereferencing the name member. When you try and operate on things[1].name you are operating on a pointer, which is simply a memory location.
This is one of the trickiest things when learning to use pointers. Here is some more reading on the dereference operator, and syntax hints.
http://en.wikipedia.org/wiki/Dereference_operator#Other_syntax
Edit:
After compiling myself, I realized that I was on the wrong track with this one, and that std::cout will correctly handle the char pointer. You should be able to solve this with your code, just ensure that you give your struct array a size.
This worked for me:
#include <iostream>
#define MAXSIZE 3
typedef struct
{
char* name;
int age;
}Thing;
Thing *things = new Thing[MAXSIZE];
int _tmain(int argc, _TCHAR* argv[])
{
char* names[MAXSIZE] = { "Alice", "Ben", "Carlos" };
int ages[MAXSIZE] = { 24, 25, 26 };
for(int i=0; i<MAXSIZE; i++)
{
things[i].name = names[i];
things[i].age = ages[i];
}
std::cout << things[1].name << std::endl;
return 0;
}