Dynamic Arrays

Started by
36 comments, last by adam17 20 years, 8 months ago
now are you sure fscanf reads until '\n' as in new line, or '\0' as in the NULL character. plus do i have to put terminating character in the search item like "%s\n"?
here is why i am asking is the following line:
//FILE *infile = fopen(filename);//string stream;fscanf(infile, "%s", stream.c_str());  

i have tried with both stream normally and stream.c_str() (as above). both ways crash on me and get the wonderful error "Access Violation."

[edited by - adam17 on August 5, 2003 11:14:18 PM]
Advertisement
the third parameter MUST be a char* if you read a string(or an array of char)

and it reads what you tell him to read.
if you tell him %d it will read only sizeof int, no matter what''s after that,
if you tell him "%d\n" it will suppose that after the int there is a newline
so to read a line you must tell him %s\n

the access violation you are getting it''s because c_str() if i don''t remember badly return the pointer to the string, but that string is empty now, so it''s not allocated anything so c_str() returns null i think, and it cannot save the string in "null"

to resolve this make a char buffer[32523634] save the string there and then copy it in the stream
in C there are not streams ( i think)


There aren''''t problems that can''''t be solved with a gun...
There aren''t problems that can''t be solved with a gun...
omg omg omg omg omg!!! i finally have this thing working after SO MANY WEEKS of struggling! thank you guys SO MUCH!!! especially you Thor82 for helping me in the last few posts. i have one more problem, which is something i can fix. my program is just rendering one box instead of the 2 boxes it needs to. o well. thank you guys once again!!
grrrr. i knew i would be back with more troubles. does anyone have any idea why fscanf could be converting values from strings to float or integer values wrong? i keep getting some integers in scientific notation.
plus another problem i have been having is getting the program to run twice in a row without giving some strange errors and range checking errors. just need some tips for stability.
can you paste some code please or send me in email:thor82 at libero.it


There aren''''t problems that can''''t be solved with a gun...
There aren''t problems that can''t be solved with a gun...
quote:Original post by Thor82
try using the stl "vector"...it''s easier and it handles everything

or try using old c calloc ( array= (int*) calloc(n,sizeof(int)))

in this way it stores n*sizeof(int) bytes...
it''s not the best way though....


There aren''t problems that can''t be solved with a gun...


That`s what the old man kenobi said to young luke
"Trust in force,Luke"

No quake3arena has got nothing to with the game or the company but everything with numerology,etc...
No quake3arena has got nothing to with the game or the company but everything with numerology,etc...
quote:Anonymous Poster
This if statement is absolutley uneccessary, as the new operator will throw an exception on failure. The code should look something like this: ...


quote:MSDN
If there is insufficient memory for the allocation request, by default operator new returns NULL. You can change this default behavior by writing a custom exception-handling routine and calling the _set_new_handler run-time library function with your function name as its argument. For more details on this recovery scheme, see The operator new Function.


quote:STL
...the first operator new will attempt to allocate memory and if it fails, will throw an exception...
How do I set my laser printer on stun?
quote:That`s what the old man kenobi said to young luke


??


There aren''''t problems that can''''t be solved with a gun...
There aren''t problems that can''t be solved with a gun...

This topic is closed to new replies.

Advertisement