-
December 2nd, 2009, 01:28 PM
#1
Assign value to element in dynamic array, turns out to be NULL
Hello,
I am developing code to
1. read the number of lines in a file ipFile, and store that value in ipLength
2. create a dynamic array ipArray of size ipLength
3. initialize ipArray
4. reposition the file pointer to the beginning of ipFile
5. get one line from ipFile, store it into ipAddress
6. assign ipAddress to an element of dynamic array ipArray
For debugging purposes, I want to display the contents of each element of the dynamic array. When i printf ipAddress, it is fine, however when I printf &ipArray[p], it turns up blank for each element. If I printf ipArray[p], it displays <null> for each element.
Below is my code, please guide:
int ipLength=0;
while (!ipFile.eof())
{//begin while !ipFile.eof()
getline (ipFile,ipLine);
ipLength++;
}//end while !ipFile.eof()
string *ipArray = NULL;
ipArray = new string [ipLength];
for (int i=0; i<ipLength; i++)
{//begin for loop
ipArray[i] = "";
}//end for loop
ipFile.seekg(0, ios::beg);
int p=0;
while (!ipFile.eof())
{//begin while !ipFile.eof()
getline (ipFile,ipLine);
int TempNumOne=ipLine.size();
char ipOutput[100]={0};
for (int a=0;a<=TempNumOne;a++)
{//begin for loop
ipOutput[a]=ipLine[a];
}//end for loop
sscanf(ipOutput, "%s", &ipAddress);
ipArray [p] = ipAddress;
printf ("%d. %s\n", p, ipArray[p]);
p++;
}//end while !ipFile.eof()
ipFile.close();
delete [] ipArray;
ipArray = NULL;
-
December 2nd, 2009, 01:35 PM
#2
Re: Assign value to element in dynamic array, turns out to be NULL
The type of ipArray[p] is a C++ std::string. Printf is a C function, and does not understand C++ classes; it expects the argument corresponding to a %s to be a const char*.
Try outputting ipArray[p].c_str(), or else do your output with cout rather than printf.
Also, I would recommend using a std::vector<std::string> rather than a dynamic array.
-
December 2nd, 2009, 01:56 PM
#3
Re: Assign value to element in dynamic array, turns out to be NULL
In the first section, you read until end of file. Once this happens, the internal
state of the stream is such that trying to use it further will fail. You need
to clear the state.
So, before doing the seekg() ...
Also, I agree with previous post ... use a vector<string> and you can do everything
in one pass thru the file.
-
December 2nd, 2009, 01:57 PM
#4
Re: Assign value to element in dynamic array, turns out to be NULL
What do noobs have against white space? Seriously, get in the habit of putting spaces between variables and operators and anywhere else you can to make your code readable.
-
December 3rd, 2009, 09:15 AM
#5
[SOLVED] Assign value to element in dynamic array, turns out to be NULL
Originally Posted by Lindley
The type of ipArray[p] is a C++ std::string. Printf is a C function, and does not understand C++ classes; it expects the argument corresponding to a %s to be a const char*.
Try outputting ipArray[p].c_str(), or else do your output with cout rather than printf.
Also, I would recommend using a std::vector<std::string> rather than a dynamic array.
Awesome, it now works, thanks!
-
December 3rd, 2009, 05:04 PM
#6
Re: Assign value to element in dynamic array, turns out to be NULL
Originally Posted by GCDEF
What do noobs have against white space? Seriously, get in the habit of putting spaces between variables and operators and anywhere else you can to make your code readable.
White space takes up space! Both on your display and on your disk!
Viggy
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|