By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,615 Members | 1,961 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,615 IT Pros & Developers. It's quick & easy.

Problem Taking input:(

P: n/a
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[i]=0;
artist[i]=0;
album[i]=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. :-(

Apr 11 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Sudip wrote:
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 */

--
If you're posting through Google read <http://cfaj.freeshell.org/google>
Apr 11 '06 #2

P: n/a
Ο/Η 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[i]=0;
artist[i]=0;
album[i]=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. :-(


Apr 11 '06 #3

P: n/a
Sudip wrote:

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[i]=0;
artist[i]=0;
album[i]=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/>
Apr 11 '06 #4

P: n/a
On 11 Apr 2006 02:35:40 -0700, "Sudip" <su*******@gmail.com> wrote:
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[i]=0;
artist[i]=0;
album[i]=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
Apr 12 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.