Can printf change its parameters?
EDIT: complete code with main one here http://codepad.org/79aLzj2H
and again it was strange behavior happening
for (i = 0; i<tab_size; i++)
{
//CORRECT OUTPUT
printf("%s\n", tableau[i].capitale);
printf("%s\n", tableau[i].pays);
printf("%s\n", tableau[i].commentaire);
//WRONG OUTPUT
//printf("%s --- %s --- %s |\n", tableau[i].capitale, tableau[i].pays, tableau[i].commentaire);
}
I have an array of the following strcuture
struct T_info
{
char capitale[255];
char pays[255];
char commentaire[255];
};
struct T_info *tableau;
This is how the array is filled
int advance(FILE *f)
{
char c;
c = getc(f);
if(c == '\n')
return 0;
while(c != EOF && (c == ' ' || c == '\t'))
{
c = getc(f);
}
return fseek(f, -1, SEEK_CUR);
}
int get_word(FILE *f, char * buffer)
{
char c;
int count = 0;
int space = 0;
while((c = getc(f)) != EOF)
{
if (c == '\n')
{
buffer[count] = '\0';
return -2;
}
if ((c == ' ' || c == '\t') && space < 1)
{
buffer[count] = c;
count ++;
space++;
}
else
{
if (c != ' ' && c != '\t')
{
buffer[count] = c;
count ++;
space = 0;
}
else /* more than one space*/
{
advance(f);
break;
}
}
}
buffer[count] = '\0';
if(c == EOF)
return -1;
return count;
}
void fill_table(FILE *f,struct T_info *tab)
{
int line = 0, column = 0;
fseek(f, 0, SEEK_SET);
char buffer[MAX_LINE];
char c;
int res;
int i = 0;
while((res = get_word(f, buffer)) != -999)
{
switch(column)
{
case 0:
strcpy(tab[line].capitale, buffer);
column++;
break;
case 1:
strcpy(tab[line].pays, buffer);
column++;
break;
default:
strcpy(tab[line].commentaire, buffer);
column++;
break;
}
/*if I printf each one alone here, everything works ok*/
//last word in line
if (res == -2)
{
if (column == 2)
{
strcpy(tab[line].commentaire, " ");
}
//wrong output here
printf("%s -- %s -- %s\n", tab[line].capitale, tab[line].pays, tab[line].commentaire);
column = 0;
line++;
continue;
}
column = column % 3;
if (column == 0)
{
line++;
}
/*EOF reached*/
if(res == -1)
return;
}
return ;
}
Edit:
try it
printf("%s -- ", tab[line].capitale);
printf("%s --", tab[line].pays);
printf("%s --\n", tab[line].commentaire);
gives me the result
-- --abi -- Emirats arabes unis
I expect to receive
Abu Dhabi -- Emirats arabes unis --
Did I miss something?
a source to share
Does printf have side effects?
Well, he's typing on the screen. This is a side effect. Other than that: no.
printf changes its parameters
No
I am getting wrong values [...], what's going on?
If by the wrong results you imply that the output isn't showing up when it should, it's probably just a buffering issue (the second version doesn't print a newline, which could result in the output not being flushed).
a source to share
It is highly unlikely that printf is your problem. Which is much, much more likely that you are corrupting memory, and your strange printf results are just a symptom.
There are several places in your code that can lead to reading or writing past the end of the array. It's hard to tell which ones might be causing problems without seeing your input, but here are a few that I've noticed:
-
get_lines_count
will not read the last line unless it ends on a new line, but your other methods will handle that line -
advance
will skip a newline if preceded by spaces, which will disable column-based processing and may cause some of your rows to be uninitialized -
get_word
does not fulfill any restrictions onbuffer
There may be others, these were the ones that jumped out at me.
a source to share
I tested your code by adding the missing pieces (constant MAX_LINE, main function and sample data file with 3 columns separated by 2+ space) and the code works as expected.
Maybe the code you posted is not yet complete (fill_table () looks for the magic number -999 from get_word (), but get_word () never returns that), your main function is missing, so we don't know you are allocating memory correctly and etc.
Unrelated but important: It is not recommended (nor does it wrap around) doing relative movements with fseek
in text files. You probably want to use ungetc
in this case. If you really want to move the file pointer while reading the text stream, you must use fgetpos
and fsetpos
.
Your approach to getting help is very wrong. You assumed that printf had side effects without even understanding your code. The problem is clearly not printf, but you didn't have any information. Your code has not been completed. You should create a minified test file that will compile and display your problem and include it in your question. Don't blame the functions of a random library if you don't understand what's really wrong with your program. The problem can be anywhere.
a source to share
From your comments, I guess if you are using these printf statements,
printf("%s\n", tableau[i].capitale);
printf("%s", tableau[i].pays);
printf("%s\n", tableau[i].commentaire);
then everything works fine ...
So, try replacing your only printf statement with this. ( Line # 173 at http://codepad.org/79aLzj2H )
printf("%s\n %s %s /n", tableau[i].capitale, tableau[i].pays, tableau[i].commentaire);
a source to share