|
kitty5 wrote: data[a] = "q";
You must allocate memory for data[a] before you can assign anything to it. Consider:
for (int a = 0; a < 200; a++)
{
data[a] = new char[5];
data[a][0] = 'q';
data[a][1] = '\0';
strcpy(data[a], "q");
} Now the fscanf() statement, sans the &, should be fine (although initialization is not necessary).
kitty5 wrote: while( (feof(fpread) == 0 || ferror(fpread) == 0) )
Use && instead.
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
<br />
<blockquote class="FQ"><div class="FQA">DavidCrow wrote:</div>You must allocate memory for data[a] before you can assign anything to it. Consider:<br />
<br />
<br />
for (int a = 0; a < 200; a++)<br />
{ <br />
data[a] = new char[5]; <br />
data[a][0] = 'q'; <br />
data[a][1] = '\0'; <br />
strcpy(data[a], "q");<br />
}</blockquote><br />
<br />
I've also added the above code. However, I'm still getting the same error.
Kitty5
|
|
|
|
|
kitty5 wrote: I've also added the above code. However, I'm still getting the same error.
But did you change the fscanf() statement, too?
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
|
Strange. It worked fine for me with only those two changes.
void main( void )
{
char *data[200];
FILE *pFile;
int i = 0;
pFile = fopen("c:\\data.txt", "r");
if (pFile != NULL )
{
for (int a = 0; a < 200; a++ )
data[a] = new char[5];
while ((feof(pFile) == 0 && ferror(pFile) == 0))
{
fscanf(pFile, "%s", data[i]);
printf("data = %s \n", data[i]);
i++;
}
fclose(pFile);
}
}
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
You actually have a couple problems:
kitty5 wrote: //initialize data
for ( int a = 0; a < 200; a++ )
{
data[a] = "q";
}
Besides being inefficient, this is actually buggy. The reason is simple: You have an array of 200 unallocated strings. You are marching through all 200 and setting each one to "q\0". Since there is no memory allocated for these pointers ... you are writing in random places (NOT GOOD!).
kitty5 wrote: fscanf(fpread, "%s", &data[i]); <- problem is here... i think
Don't use %s with scanf functions. In most cases, it won't do what you hope it will. Instead, use fread, or fgetc. A slightly better solution would be to read in the entire buffer at once (decreasing your harddrive accesses):
int _tmain(int argc, _TCHAR* argv[])
{
char *data[200] = {0};
char* buffer = NULL;
FILE *fpread;
int i = 0;
if ((fpread = fopen("data.txt", "r")) == NULL)
{
printf("Cannot open file for reading. \n");
return -1;
}
if (0 != fseek(fpread, 0, SEEK_END))
{
printf("Cannot seek end of file.\n");
fclose(fpread);
return -1;
}
size_t fileSize = ftell(fpread);
if (0 != fseek(fpread, 0, SEEK_SET))
{
printf("Failed to seek beginning of file.\n");
fclose(fpread);
return -1;
}
buffer = new char[fileSize + 1];
memset(buffer, 0, fileSize + 1);
if (fileSize != fread(buffer, sizeof(char), fileSize, fpread))
{
printf("Failed to read file.\n");
fclose(fpread);
delete [] buffer;
buffer = NULL;
return -1;
}
delete [] buffer;
buffer = NULL;
return 0;
}
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
-- modified at 16:33 Thursday 29th June, 2006
|
|
|
|
|
Zac Howland wrote: if (fileSize != fread(buffer, sizeof(char), fileSize, fpread))
{
printf("Failed to read file.\n");
fclose(fpread);
delete [] buffer;
buffer = NULL;
return -1;
}
Does the file need to be written in a special format?
I created it using notepad and saved it as: data.txt
When I try your code I get: Failed to read file.
This means that the number of items actually read (this is wha fread() returns) is not = to the fileSize.
This is true. So I don't understand why we're comparing the fileSize fo the number of items being read....
Kitty5
|
|
|
|
|
fileSize is the number of bytes in the file. You can place a breakpoint at that line to see what value is actually being returned and take a look at the contents of the buffer. However, the return value should be equal to fileSize if read completely. fread returns the number of "items" (which in this case is the same as the number of bytes) it reads.
You can read/write it in any format. However, in this case, you are trying to read a text file, so you will want to specify that in the fopen call.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
size_t size;<br />
size = fread(buffer, sizeof(char), fileSize, fpread);<br />
printf("fileSize = %d\n", fileSize);<br />
printf("fread() = %d\n", size);
fileSize is = 96
fread() spits out = 80 items read.
the file is:
0x00
0x01
0x02
0x03
0x04
0x05
0x06
0x07
0x08
0x09
0x0a
0x0b
0x0c
0x0d
0x0e
0x0f
I understand where 80 comes from... there are 80 total characters including the \n.
Kitty5
|
|
|
|
|
The extra 16 characters are coming from the \r's that are attached to the \n's. If you want, you can just ignore the check for the size and use the buffer as is. There is a way to tell the f-functions to read \r's as well, but I can't remember how off the top of my head.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
int i = 0;
data[i] = strtok( buffer, " " );
printf("data[%d] = %s\n", i, data[i]);
do
{
i++;
data[i] = strtok( NULL, " " );
printf("data[%d] = %s\n", i, data[i]);
}while(data[i] && i<200);
how does one get strtok() to ignore a newline character?
Kitty5
-- modified at 17:59 Thursday 29th June, 2006
|
|
|
|
|
One way is to do a strtok on the '\n' character itself, which will give you a set of 0xXX strings.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Here's how I would do it:
-------------------------
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
int main(int argc, char* argv[])
{
using namespace std;
// Open the file for reading.
ifstream ifs("C:\\a.txt");
if (!ifs)
{
cerr << "Unable to open file" << endl;
return 1;
}
// Read in the contents of the file.
typedef string type_t; // You can change the type of the data here.
typedef vector<type_t> container_t; // The collection we use to store the data we read.
container_t FileContents;
copy(
istream_iterator<type_t>(ifs), istream_iterator<type_t>(),
back_inserter(FileContents)
);
// Write the contents of the container to the console.
copy(
FileContents.begin(), FileContents.end(),
ostream_iterator<type_t>(cout, "\n")
);
return 0;
}
-------------------------
Changing a single line as follows reads and stores the data as ints instead or strings:
typedef int type_t; // You can change the type of the data here.
Steve
|
|
|
|
|
A static variable is local to particular function. However, it is only initialised once (on the first call to function).
Also the value of the variable on leaving the function remains intact. On the next call to the function the the static variable has the same value as on leaving.
This is the definition I got on one of the websites
Static sounds like a CONSTANT. Replacing static with a constant sounds like the same thing.
Does that make sense to you guys????
-- modified at 14:41 Thursday 29th June, 2006
|
|
|
|
|
Jay03 wrote: Does that make sense to you guys????
not at all !!
void inc() {
static iCpt = 0;
iCpt++;
printf("%d\n", iCpt);
}
void main(void) {
for (int i = 0; i < 10; i++) {
inc();
}
}
this code actually prints :
1
2
3
4
...
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
|
|
|
|
|
Jay03 wrote: Static sounds like a CONSTANT.
No. They are different. I think you misunderstood "it is only initialised once". Look at this example:
YourFunction()
{
static int st = 0;
st++;
}
You sound pretty new to C/C++. Get a book and study it more systematically.
Best,
Jun
|
|
|
|
|
Jay03 wrote: Static sounds like a CONSTANT.
Not even close. When a variable has static duration, it is allocated when the program begins and deallocated when the program ends. A const variable specifies that the variable is not modifiable.
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
I am using visual c++ 6.0;
I want to print numbers with thousand separator, what format to use.
I have float variables that i want to print like these:
252,000.00
150,250.25
What to do ?
|
|
|
|
|
If this is with a Win32 application, use GetNumberFormat() . Otherwise, you'll need to roll your owwn.
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
|
Hi all,
I downloaded EasyInstaller2 and NSIS, too,
but they are too difficult to me.
Could anybody explain to me how to use
one of the two to create an installer for
my Visual C++/MFC .NET project ?
Thanx in advance,
Desmo16.
|
|
|
|
|
How about XCOPY ?; After all, .Net was touting "the end of DLL Hell" and "XCOPY deployment"...
Seriously, I am not familiar with either of those installers - I use Wise.
Peace!
-=- James If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! DeleteFXPFiles & CheckFavorites (Please rate this post!)
|
|
|
|
|
Hi,I want to write COM dll..for serial port communication of an instrument,i want write code to open,close,read and write data to port in the DLL.I want valueable suggestions..guidence..to start..
I request any existing articles on this..so that they can be useful to me..
thanks in before
James
-- modified at 13:01 Thursday 29th June, 2006
|
|
|
|
|
Start here.
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
thanks for the response..David is there any sample application..for the given article
|
|
|
|