Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > strange behaviour

Reply
Thread Tools

strange behaviour

 
 
gribouille
Guest
Posts: n/a
 
      10-15-2006
Hi,

via fgets() i create a array containing a text file.

fp = fopen(argv[1], "r");
while ((c = fgetc(fp)) != EOF) {
line[l] = c;


when i want to print it
while (i < w){
//printf("%c", bodytext[i]);
i++;
}
it works, but if i intergrate a if(line[i]=='\\') {...}
it print me some funny caracteres in the text

what happened ?
 
Reply With Quote
 
 
 
 
Ico
Guest
Posts: n/a
 
      10-15-2006
gribouille <(E-Mail Removed)> wrote:

> via fgets() i create a array containing a text file.
>
> fp = fopen(argv[1], "r");
> while ((c = fgetc(fp)) != EOF) {
> line[l] = c;


I see no fgets() in this snippet of code

> when i want to print it
> while (i < w){
> //printf("%c", bodytext[i]);
> i++;
> }


This does not print anything, since the line containing printf() is
commented out.

> it works, but if i intergrate a if(line[i]=='\\') {...}
> it print me some funny caracteres in the text


Your code is incoherent, does not match your description and does not
compile. Please provide a complete and compiling program showing the
behaviour you see.

> what happened ?


I can't tell from this description. Maybe others can.

--
:wq
^X^Cy^K^X^C^C^C^C
 
Reply With Quote
 
 
 
 
gribouille
Guest
Posts: n/a
 
      10-15-2006
may be this one is better ...


FILE *fp;


line = (char *)malloc(2*sizeof(char));
bodytext = (char *)malloc(sizeof(char));
docwidth = (char *)malloc(sizeof(char));
//tag = (char *)malloc(sizeof(strlen(line)));

fp = fopen(argv[1], "r");
while ((c = fgetc(fp)) != EOF) {
line[l] = c;
allocation_memory(1); // realloc function
//printf("%c", line[l]);
l++;
}


fclose(fp);

i=0;
w = strlen(bodytext);
while (i < w){
printf("%c", bodytext[i]);
i++;
}

realloc funtcion

if ((line = realloc(line, sizeOfLine))) {
/* realoc ok */
sizeOfLine = sizeOfLine++;
} else {

printf ("*** No Memory\n");
exit (1);
free (line);
}



// so if i put the condition in the while loop, it display some wierds
caractere
 
Reply With Quote
 
Ico
Guest
Posts: n/a
 
      10-15-2006
gribouille <(E-Mail Removed)> wrote:

> may be this one is better ...


No, I don't mean to be picky, but it is not better. It still is not
complete and it still does not compile. Please provide a *complete* C
program which people can feed to their compiler without having to guess
what you mean from your snippets. Complete C programs #include the
headers they need, and provide at least a main() function.

[snipped code]

--
:wq
^X^Cy^K^X^C^C^C^C
 
Reply With Quote
 
gribouille
Guest
Posts: n/a
 
      10-15-2006
this one is complete and compile, sorry about that i'm new on this forum

to test it just put text file in the commande line.
So it works well if i don't put the line
if (line[i] == '\\'){ printf("hello");}



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
char *line;
void allocation_memory(int);

int main(int argc, char *argv[]){

line = (char *)malloc(2*sizeof(char));
char c;
int i = 0;
int v = 0;
int l = 0;
int w = 0;

FILE *fp;
fp = fopen(argv[1], "r");
while ((c = fgetc(fp)) != EOF) {
line[l] = c;
allocation_memory(1);
l++;
}


fclose(fp);

i=0;
w = strlen(line);
while (i < w){
if (line[i] == '\\'){
printf("hello");
}
printf("%c", line[i]);
i++;
}
free(line);
}

void allocation_memory(int grade)
{

int sizeOfLine = 3;
int sizeOfbodytext = 2;
int sizeOfdocwidth = 2;
switch( grade )
{
case '1' : if ((line = realloc(line, sizeOfLine))) {
/* la réallocation s'est bien passée , l'affectation est
sûre: */
sizeOfLine = sizeOfLine++;
} else {

printf ("*** Mémoire insuffisante\n");
exit (1);
free (line);
}
// return NULL;
break;

}
}
 
Reply With Quote
 
Ancient_Hacker
Guest
Posts: n/a
 
      10-15-2006
Lots of things wrong with your code!

> line = (char *)malloc(2*sizeof(char));
> char c;
> int i = 0;
> int v = 0;
> int l = 0;
> int w = 0;
>
> FILE *fp;
> fp = fopen(argv[1], "r"); // you don't check to see if the fopen worked


> while ((c = fgetc(fp)) != EOF) {
> line[l] = c; // the letter "ell" is a poor name for a variable, looks too much line the digit "1"
> allocation_memory(1); // reallocating a byte at a time is ridiculously slow
> l++;
> }
>
>
> fclose(fp);
>



> i=0;


> w = strlen(line); // you never put a terminating zero byte into line??
> while (i < w){
> if (line[i] == '\\'){
> printf("hello");
> }


> printf("%c", line[i]);
> i++;
> }



> void allocation_memory(int grade)
> {
>
> int sizeOfLine = 3; // this gets done EVERY time you call this function!
> int sizeOfbodytext = 2;
> int sizeOfdocwidth = 2;
> switch( grade )
> {
> case '1' : if ((line = realloc(line, sizeOfLine))) {
> /* la réallocation s'est bien passée , l'affectation est
> sûre: */
> sizeOfLine = sizeOfLine++; // useless, and redundant at the same time.
> } else {
>
> printf ("*** Mémoire insuffisante\n");
> exit (1);
> free (line);
> }
> // return NULL;
> break;
>
> }
> }


--------------

I suggest you change your memory allocation scheme to get the size of
the file first, then do just one malloc() to get that much memory.

 
Reply With Quote
 
gribouille
Guest
Posts: n/a
 
      10-15-2006
i won' t get the size till the end of the file, is there another way of
getting it ?

Ancient_Hacker a écrit :
> Lots of things wrong with your code!
>
>> line = (char *)malloc(2*sizeof(char));
>> char c;
>> int i = 0;
>> int v = 0;
>> int l = 0;
>> int w = 0;
>>
>> FILE *fp;
>> fp = fopen(argv[1], "r"); // you don't check to see if the fopen worked

>
>> while ((c = fgetc(fp)) != EOF) {
>> line[l] = c; // the letter "ell" is a poor name for a variable, looks too much line the digit "1"
>> allocation_memory(1); // reallocating a byte at a time is ridiculously slow
>> l++;
>> }
>>
>>
>> fclose(fp);
>>

>
>
>> i=0;

>
>> w = strlen(line); // you never put a terminating zero byte into line??
>> while (i < w){
>> if (line[i] == '\\'){
>> printf("hello");
>> }

>
>> printf("%c", line[i]);
>> i++;
>> }

>
>
>> void allocation_memory(int grade)
>> {
>>
>> int sizeOfLine = 3; // this gets done EVERY time you call this function!
>> int sizeOfbodytext = 2;
>> int sizeOfdocwidth = 2;
>> switch( grade )
>> {
>> case '1' : if ((line = realloc(line, sizeOfLine))) {
>> /* la réallocation s'est bien passée , l'affectation est
>> sûre: */
>> sizeOfLine = sizeOfLine++; // useless, and redundant at the same time.
>> } else {
>>
>> printf ("*** Mémoire insuffisante\n");
>> exit (1);
>> free (line);
>> }
>> // return NULL;
>> break;
>>
>> }
>> }

>
> --------------
>
> I suggest you change your memory allocation scheme to get the size of
> the file first, then do just one malloc() to get that much memory.
>

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      10-15-2006
gribouille wrote:
>
> i won' t get the size till the end of the file, is there another way of
> getting it ?


Don't top-post. Your answer belongs after the /snipped/ material
you quote (or possibly intermixed). The snipping removes any
quotes not relevant to your reply.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

 
Reply With Quote
 
Ico
Guest
Posts: n/a
 
      10-15-2006
gribouille <(E-Mail Removed)> wrote:

> this one is complete and compile, sorry about that i'm new on this
> forum


No problem, but you'll see people are much more eager to help you when
you post some code that is complete and actually runs.

> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> #include <ctype.h>
> char *line;
> void allocation_memory(int);
>
> int main(int argc, char *argv[]){
>
> line = (char *)malloc(2*sizeof(char));


Hint: No need to cast the return value of malloc.

> char c;
> int i = 0;
> int v = 0;
> int l = 0;
> int w = 0;
>
> FILE *fp;
> fp = fopen(argv[1], "r");


Hint: You use argv[1] here, without checking if there are actually
additional arguments supplied to the program. Might crash.

Hint: You don't check the value of fp after the fopen call. This causes
undefined behaviour when you use the variable. Might crash.

> while ((c = fgetc(fp)) != EOF) {
> line[l] = c;
> allocation_memory(1);
> l++;
> }
>
>
> fclose(fp);
>
> i=0;
> w = strlen(line);


This is very wrong: you are using strlen to find out the length of the
string, but your buffer of characters is not terminated by a '\0', which
does not make it a valid C-string. the strlen function is very likely to
return a value that is larger then the number of characters you have
allocated previously, thus your code is reading memory you do not own.
The fix would be to make sure the last character in your string is
always a '\0'

> while (i < w){
> if (line[i] == '\\'){
> printf("hello");
> }
> printf("%c", line[i]);
> i++;
> }
> free(line);


Hint: main() should return and int

> }
>
> void allocation_memory(int grade)
> {
>
> int sizeOfLine = 3;
> int sizeOfbodytext = 2;
> int sizeOfdocwidth = 2;
> switch( grade )
> {
> case '1' : if ((line = realloc(line, sizeOfLine))) {
> /* la réallocation s'est bien passée , l'affectation est
> sûre: */
> sizeOfLine = sizeOfLine++;


This line yields undefined behaviour, the proper syntax is either
"sizeOfLine = sizeOfLine + 1;" or "sizeOfLine ++;"

> } else {
>
> printf ("*** Mémoire insuffisante\n");
> exit (1);
> free (line);
> }
> // return NULL;
> break;
>
> }
> }


There might be more problems with this code I, but these ones I spotted
in the first seconds.


--
:wq
^X^Cy^K^X^C^C^C^C
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-15-2006
"Ancient_Hacker" <(E-Mail Removed)> writes:
> Lots of things wrong with your code!
>
>> line = (char *)malloc(2*sizeof(char));
>> char c;
>> int i = 0;
>> int v = 0;
>> int l = 0;
>> int w = 0;
>>
>> FILE *fp;
>> fp = fopen(argv[1], "r"); // you don't check to see if the fopen worked

>
>> while ((c = fgetc(fp)) != EOF) {
>> line[l] = c; // the letter "ell" is a poor name for a variable, looks too much line the digit "1"
>> allocation_memory(1); // reallocating a byte at a time is ridiculously slow

[snip]

If you're going to comment on code, please write your comments
separately, as new text, not as actual comments added to the code.
It's impossible to tell, without going back to the previous article,
whether your comments were in the originally posted code or not.

And "//" comments on Usenet are ill-advised, especially with very long
lines.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
debugger behaviour different to execution behaviour Andy Chambers Java 1 05-14-2007 09:51 AM
a strange behaviour of FF Maurice Firefox 2 03-11-2005 11:53 PM
Strange mouse behaviour with flash in Mozilla 1.3.7 hpoppe Firefox 0 11-07-2004 12:16 PM
Strange taskbar behaviour (notification area) Falcon Wireless Networking 0 08-17-2004 09:03 AM
[mozilla1.6] strange behaviour with newsgroup joost68 Firefox 5 04-03-2004 03:48 AM



Advertisments