Problem Taking input:(

S

Sudip

I'm new in c programming. I am writing a program which reads the ID3 V1
tag from a mp3 file and edits it. But everytime i try to take inputs ,
the first character of my album contains 0. So, it doesn't write
anything at the start of album field. So, winamp cannot recognize the
album name. Can someone pls help me? :-(

The code for tag input and edit is:

void edittag(long loc,char name[]){
FILE *fp;
char song[30],artist[30],album[30],year[4],comment[28],track;
char genre;
int i,j;
for(i=0;i<30;i++){
song=0;
artist=0;
album=0;
}
for(j=0;j<28;j++){
comment[j]=0;
}
fflush(stdin);
printf("\nTitle: ");
gets(song);
printf("\nArtist: ");
gets(artist);
printf("\nAlbum: ");
gets(album);
printf("\nYear: ");
gets(year);
printf("\nComment: ");
gets(comment);
printf("\nTrack: ");
scanf("%d",&track);
printf("\nGenre: ");
scanf("%d",&genre);
fp=fopen(name,"rb+");
for(i=loc-125,j=0;i<loc-95,j<30;i++,j++){
fseek(fp,i,SEEK_SET);
fputc(song[j],fp);
}
for(i=loc-95,j=0;i<loc-65,j<30;i++,j++){
fseek(fp,i,SEEK_SET);
fputc(artist[j],fp);
}
for(i=loc-65,j=0;i<loc-35,j<30;i++,j++){
fseek(fp,i,SEEK_SET);
fputc(album[j],fp);
}
for(i=loc-35,j=0;i<loc-31,j<4;i++,j++){
fseek(fp,i,SEEK_SET);
fputc(year[j],fp);
}
for(i=loc-31,j=0;i<loc-3,j<28;i++,j++){
fseek(fp,i,SEEK_SET);
fputc(comment[j],fp);
}
fseek(fp,loc-3,SEEK_SET);
fputc(00,fp);
fseek(fp,loc-2,SEEK_SET);
fputc(track,fp);
fseek(fp,loc-1,SEEK_SET);
fputc(genre,fp);
}

pls someone help me. :-(
 
P

Pedro Graca

Sudip said:
I'm new in c programming. I am writing a program which reads the ID3 V1
tag from a mp3 file and edits it. But everytime i try to take inputs ,
the first character of my album contains 0. So, it doesn't write
anything at the start of album field. So, winamp cannot recognize the
album name. Can someone pls help me? :-(

[snip code with gets() and no error-checking]
pls someone help me. :-(

Hmmmmm ... "the first character of my album contains 0";

do you mean it contains 0 before or after you change it?
if it's before you change it I see no problem;
if it's after you change it, then simply change it to something other
than 0.

pseudo-code follows:

/* before you update the file, do */
if (album[0] == 0) {
album[0] = SOMETHING_ELSE;
}
/* now update the file */
 
S

stathisgotsis

Ο/Η Sudip έγÏαψε:
I'm new in c programming. I am writing a program which reads the ID3 V1
tag from a mp3 file and edits it. But everytime i try to take inputs ,
the first character of my album contains 0.

Have you tried to print out the string stored in album[]? If what you
say is true, it is an empty string.
So, it doesn't write
anything at the start of album field. So, winamp cannot recognize the
album name. Can someone pls help me? :-(

The code for tag input and edit is:

void edittag(long loc,char name[]){
FILE *fp;
char song[30],artist[30],album[30],year[4],comment[28],track;
char genre;
int i,j;
for(i=0;i<30;i++){
song=0;
artist=0;
album=0;
}
for(j=0;j<28;j++){
comment[j]=0;
}


This is not necessary as you store strings in the above arrays later
on. gets() will null-terminate the strings.
fflush(stdin);

This is non-standard, fflush() works on output streams. If you want to
eat up the rest of stdin you should look for a function called
flushln() which has been published here often. It looks like this:

int flushln(FILE *f)
{
int ch;
while ((EOF != (ch = fgetc(f))) && ('\n' != ch)) ;
return ch;
}
printf("\nTitle: ");
gets(song);

You should avoid using gets() because it can lead to buffer overruns,
consider fgets() instead.
printf("\nArtist: ");

stdout is probably line-buffered so you should use something:
fflush(stdout) after this call.
gets(artist);
printf("\nAlbum: ");
gets(album);
printf("\nYear: ");
gets(year);
printf("\nComment: ");
gets(comment);
printf("\nTrack: ");
scanf("%d",&track);

Consider the return value from scanf() and remember that you have at
least one newline character left in stdin. You should possibly use
flushln() after this call to scanf() to flush the stdin.
printf("\nGenre: ");
scanf("%d",&genre);

Same as above.
fp=fopen(name,"rb+");

Check the return value from fopen(), it could fail.
for(i=loc-125,j=0;i<loc-95,j<30;i++,j++){
fseek(fp,i,SEEK_SET);
fputc(song[j],fp);
}
for(i=loc-95,j=0;i<loc-65,j<30;i++,j++){
fseek(fp,i,SEEK_SET);
fputc(artist[j],fp);
}
for(i=loc-65,j=0;i<loc-35,j<30;i++,j++){
fseek(fp,i,SEEK_SET);
fputc(album[j],fp);
}
for(i=loc-35,j=0;i<loc-31,j<4;i++,j++){
fseek(fp,i,SEEK_SET);
fputc(year[j],fp);
}
for(i=loc-31,j=0;i<loc-3,j<28;i++,j++){
fseek(fp,i,SEEK_SET);
fputc(comment[j],fp);
}
fseek(fp,loc-3,SEEK_SET);
fputc(00,fp);
fseek(fp,loc-2,SEEK_SET);
fputc(track,fp);
fseek(fp,loc-1,SEEK_SET);
fputc(genre,fp);
}

pls someone help me. :-(
 
C

CBFalconer

Sudip said:
I'm new in c programming. I am writing a program which reads the ID3 V1
tag from a mp3 file and edits it. But everytime i try to take inputs ,
the first character of my album contains 0. So, it doesn't write
anything at the start of album field. So, winamp cannot recognize the
album name. Can someone pls help me? :-(

The code for tag input and edit is:

void edittag(long loc,char name[]){
FILE *fp;
char song[30],artist[30],album[30],year[4],comment[28],track;
char genre;
int i,j;
for(i=0;i<30;i++){
song=0;
artist=0;
album=0;
}
for(j=0;j<28;j++){
comment[j]=0;
}
fflush(stdin);
printf("\nTitle: ");
gets(song);


You already have at least two fatal errors. fflush only functions
on output files, applying it to input files results in undefined
behaviour. It is also impossible to use gets() safely. You could
use fgets(), or my replacement for gets, ggets(), which is
available at:

<http://cbfalconer.home.att.net/download/ggets.zip>

as portable standard C source.

Since you are posting from the foully broken google interface to
usenet, read my sig and the referenced URLs before posting again,
or replying. Google is NOT usenet.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
B

Barry Schwarz

I'm new in c programming. I am writing a program which reads the ID3 V1
tag from a mp3 file and edits it. But everytime i try to take inputs ,
the first character of my album contains 0. So, it doesn't write
anything at the start of album field. So, winamp cannot recognize the
album name. Can someone pls help me? :-(

The code for tag input and edit is:

void edittag(long loc,char name[]){
FILE *fp;
char song[30],artist[30],album[30],year[4],comment[28],track;
char genre;
int i,j;
for(i=0;i<30;i++){
song=0;
artist=0;
album=0;
}
for(j=0;j<28;j++){
comment[j]=0;
}
fflush(stdin);


fflush is not defined for input streams, only output.
printf("\nTitle: ");
gets(song);

gets() can lead to buffer overflow.
printf("\nArtist: ");
gets(artist);
printf("\nAlbum: ");
gets(album);
printf("\nYear: ");
gets(year);
printf("\nComment: ");
gets(comment);
printf("\nTrack: ");
scanf("%d",&track);

%d tells scanf that the corresponding argument is an int*. You
provided a char*. Lie to the compiler and invoke undefined behavior.
You are overwriting memory.
printf("\nGenre: ");
scanf("%d",&genre);
Ditto.

fp=fopen(name,"rb+");
for(i=loc-125,j=0;i<loc-95,j<30;i++,j++){

The two relational expressions in the second clause do not do what you
think. The first is evaluated and then discarded. Then the second is
evaluated and that value is the "result" of the clause. Did you mean
&& instead of comma. Besides, one of them is redundant.
fseek(fp,i,SEEK_SET);

Why do you feel the need to seek to the next sequential position?
Wouldn't one seek before the for loop be sufficient?
fputc(song[j],fp);
}
for(i=loc-95,j=0;i<loc-65,j<30;i++,j++){
fseek(fp,i,SEEK_SET);
fputc(artist[j],fp);
}
for(i=loc-65,j=0;i<loc-35,j<30;i++,j++){
fseek(fp,i,SEEK_SET);
fputc(album[j],fp);
}
for(i=loc-35,j=0;i<loc-31,j<4;i++,j++){
fseek(fp,i,SEEK_SET);
fputc(year[j],fp);
}
for(i=loc-31,j=0;i<loc-3,j<28;i++,j++){
fseek(fp,i,SEEK_SET);
fputc(comment[j],fp);
}
fseek(fp,loc-3,SEEK_SET);
fputc(00,fp);
fseek(fp,loc-2,SEEK_SET);
fputc(track,fp);
fseek(fp,loc-1,SEEK_SET);
fputc(genre,fp);
}

pls someone help me. :-(


Remove del for email
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top