Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File I/O help?!?!?!?! 1

Status
Not open for further replies.

markwalker84

Programmer
Jul 18, 2006
6
GB
Hi there.

Firstly:

I am using an XP machine running SP2.
I am using Microsoft Visual C++ 2005 Express Edition.
I am new to VC++ (have a base in C)
I click New -> Project -> CLR -> Windows Form Application
I use the template style files that VC++ gives me which include;

Form1.h
resource.h
stdafx.h
app.ico
app.rc
AssemblyInfo.cpp
"Project_Name".cpp
stdafx.cpp
ReadMe.txt

So - now that everyone knows where i am coming from hopefully i can ask a question more effectively :)

I am trying to continually read in 3 lines from a text file.

Text.1
Number.1
Number.1
Text.2
Number.2
Number.2
(...)

and then store each to an array

Text[] - should this be String, std::string, char Text[][].......??????
num_1[]
num_2[]


So far all the code i have written has been added to Form1.h in the form of event handlers, and also a small .h file which contains my definitions of the arrays i want to store this text file information into (as well as a couple of other variables).

I would then like to be able to use those stored array values at a later date - for example; outting the text value to a label, or using the numerical value on a slide bar - just anything really.

I simply cannot work out how to do this :-( I have googled for about 2 full working days now and nothing seems to work. I have been trying fin >> & InFile >> with the appropriate #include etc but consistantly get errors along the lines of;

LNK4248: unresolved typeref token (0100004D) .....
LNK2020: unresolved token "int * _bristles"
LNK2021: unresolved external symbol "int * _bristles"

( this confuses me as the variable "_bristles" is defined in my added header file as "int _bristles[]" )

If anyone can spill any light on this issue whatso ever i would be eternally grateful!


All the best


Mark
 
Where is _bristles array definition? You must place somewhere in your code (in cpp, not h file):
Code:
int* _bristles[] = { ...,...,...,... }; // pointer values
Or define it as
Code:
int* _bristles[SIZE];
You have _bristles declaration only (with external?).
 
I have my main programme "control_programme.cpp"

which looks like this;


<CODE>
// Control Programme - Mark Walker

#inlcude "tractor_types.h"

#include "stdafx.h" // These 2 were given by the 'New #include "Form1.h" // project' wizard


using namespace control_programme

[STAThreadAttribute]

int main(array<System::String ^> ^args)
{
// Enabling Windows XP visual effects before any controls are created

Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);

// Create the main window and run it

Application::Run(gcnew Form1());

return 0;

}

</CODE>

and in "tractor_types.h" i declare the variables / arrays i want to be using to store the TEXT file information;

<CODE>
// Tractor Types

#include<string>
#include<iostream> // Think this might be left over form a previous attempt
#include<fstream>

using namespace std;

int num_Tractors = 0;
int num_param = 3 ;

int _bristles[];
int _pistons[] ;

std::string tractor_name[];

</CODE>


I dont know if that clears anything up??

Surely the definition of _bristles is effectively in control_programme.cpp as it is included?

As you can tell - i am somewhat lost,

Thanks for the help.


Mark
 
Every C or C++ variable must have one and only one definition (of a type, a size and a contents). It's not C and C++ aerobatics, it's ABC issue...
Code:
int _bristles[];
is not a definition: it's a declaration only. You say (to compiler): I have a global array of int with unknown (in this place) size but I define it (with size) in another place (source module) - let me access to this array and go on...
Compiler does not know how to allocate space for this array.
So you must define _bristles in one and only one cpp file like this (for example):
Code:
int _bristles[100];
Your compiled program has a reference to the global object _bristles, but no this object definition, so you have linker message(s).
Just imagine it's your work to compile the program. You must allocate an array with unknown size...
 
You absolute beauty!

I can't belive i was being so dumb!

Thankyou so much for spotting that - you wouldn't belive how many different sites i have signed up for trying to find an answer to that question!

Thanks again!


Mark
 
Also,

1. Dont add definitions to header (.h) files. If you include the same header file to another .cpp file, which is normal, you'll get redefinition problems. Header files are for declarations ONLY, unless you really know what you're doing.

2. For declaration of an array, or definition of an unknown size array I suggest using pointer syntax: int* _bristles;
Although you can use old style pointers, arrays in VC++ 7, you'd better start learning to use objects (String, Array, etc.) - this will make your life easier as being new in the world of programming.

------------------
When you do it, do it right.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top