这是我所做的全部代码,我试图创建一个歌曲库,将用户输入的内容放入文件中。现在编译器说,传递strcpy的参数2会使指针从integer变成不带强制转换的指针,我不知道为什么。你也可以检查我的链表中的结构。我不在链接列表中:(
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
struct node {
//definition of struct node to create struct node song
int SongID;
char Title[100];
char Artist[100];
char Composer[100];
char Album[100];
char Genre[100];
int Rating;
char Remarks[1000];
struct node*next;
};
add_song(int SongID, char Title, char Artist, char Composer, char Album, char Genre, int Rating, char Remarks) {
//this is the add song function as stated in the mp2 specs
FILE*fp;
fp=fopen("song.txt","r+");
int i=1, j, choice;
struct node* temp=malloc(sizeof(struct node));
temp->SongID=SongID;
fprintf(fp,"%d",SongID);
strcpy(temp->Title, Title);
fprintf(fp,"%s",Title);
strcpy(temp->Artist, Artist);
fprintf(fp,"%s",Artist);
strcpy(temp->Composer, Composer);
fprintf(fp,"%s",Composer);
strcpy(temp->Album, Album);
fprintf(fp,"%s",Album);
strcpy(temp->Genre, Genre);
fprintf(fp,"%s",Genre);
temp->Rating=Rating;
fprintf(fp,"%d",Rating);
strcpy(temp->Remarks, Remarks);
fprintf(fp,"%s",Remarks);
fclose(fp);
}
int main ()
{
struct node song;
int choice;
int k, i;
int SongID;
char Title[100];
char Artist[100];
char Composer[100];
char Album[100];
char Genre[100];
int Rating;
char Remarks[1000];
/* do
{
printf("Enter 1 to add song, 2 to update song, or 3 to list songs: ");
scanf("%dn", &choice1);
if (choice1==1)
{*/
srand(time(NULL));
song.SongID=rand();
printf("Enter Title: ");
fgets(Title,100,stdin);
printf("Enter Artist: ");
fgets(Artist,100,stdin);
printf("Enter Composer: ");
fgets(Composer,100,stdin);
printf("Enter Album: ");
fgets(Album,100,stdin);
//for easier code, numbers are being chosen as input
printf("Press 1 for Art Music, 2 for Popular Music, or 3 for Traditional Music): ");
scanf("%d", &choice);
if (choice==1)
{
strcpy(song.Genre,"Art Music");
}
else if (choice==2)
{
strcpy(song.Genre,"Popular Music");
}
else if (choice==3)
{
strcpy(song.Genre,"Traditional Music");
}
else
{
printf("You entered a blank genre.n");
}
printf("Enter your rating, choose from 1-5: ");
scanf("%d", &Rating);
printf("Enter Remarks: ");
fgets(Remarks,1000,stdin);
add_song(SongID, Title, Artist, Composer, Album, Genre, Rating, Remarks);
/*k=0;
break;
}
else if (choice1==2)
{
//update_song(song);
k=0;
break;
}
else if (choice1==3)
{
// list_songs(song);
k=0;
break;
}
else
{
k=1;
printf("That is not a valid input.n");
}
}while (k==1);*/
return 0;
}
您的函数定义与您传递的参数不匹配。它应该是
void add_song(int SongID, char *Title, char *Artist, char *Composer,
char *Album, char *Genre, int Rating, char *Remarks) {
...
...
}
另一个问题是songID
在main()
中未初始化。从未初始化的变量中读取是未绑定的行为。
您可能面临的另一个问题是,如果有可用空间,fgets()
会将新行n
读取到缓冲区中,这可能会有问题。需要注意的事项,必要时需要修剪。
这是您代码的工作副本,除了一件事"Remark"。在执行scanf for Remark之前,进程退出。像我在这里所做的那样更改您的参数,并将fopen模式更改为+w,这样如果文件不存在,它可以自动创建。strcpy原型是`char*strcpy(char*dest,constchar*src)。所以我相应地改变了
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
struct node {
//definition of struct node to create struct node song
int SongID;
char Title[100];
char Artist[100];
char Composer[100];
char Album[100];
char Genre[100];
int Rating;
char Remarks[1000];
struct node *next;
};
void add_song(int SongID, char *Title, char *Artist, char *Composer, char *Album, char *Genre, int Rating, char *Remarks) {
//this is the add song function as stated in the mp2 specs
FILE *fp;
fp=fopen("song.txt","a+");
int i=1, j, choice;
struct node* temp=malloc(sizeof(struct node));
temp->SongID=SongID;
fprintf(fp,"%d",SongID);
strcpy(temp->Title, Title);
fprintf(fp,"%s",Title);
strcpy(temp->Artist, Artist);
fprintf(fp,"%s",Artist);
strcpy(temp->Composer, Composer);
fprintf(fp,"%s",Composer);
strcpy(temp->Album, Album);
fprintf(fp,"%s",Album);
strcpy(temp->Genre, Genre);
fprintf(fp,"%s",Genre);
temp->Rating=Rating;
fprintf(fp,"%d",Rating);
strcpy(temp->Remarks, Remarks);
fprintf(fp,"%s",Remarks);
fclose(fp);
}
int main ()
{
struct node song;
int choice;
int k, i;
int SongID;
char Title[100];
char Artist[100];
char Composer[100];
char Album[100];
char Genre[100];
int Rating;
char Remarks[1000];
/* do
{
printf("Enter 1 to add song, 2 to update song, or 3 to list songs: ");
scanf("%dn", &choice1);
if (choice1==1)
{*/
srand(time(NULL));
song.SongID=rand();
printf("Enter Title: ");
fgets(Title,100,stdin);
printf("Enter Artist: ");
fgets(Artist,100,stdin);
printf("Enter Composer: ");
fgets(Composer,100,stdin);
printf("Enter Album: ");
fgets(Album,100,stdin);
//for easier code, numbers are being chosen as input
printf("Press 1 for Art Music, 2 for Popular Music, or 3 for Traditional Music): ");
scanf("%d", &choice);
if (choice==1)
{
strcpy(song.Genre,"Art Music");
}
else if (choice==2)
{
strcpy(song.Genre,"Popular Music");
}
else if (choice==3)
{
strcpy(song.Genre,"Traditional Music");
}
else
{
printf("You entered a blank genre.n");
}
printf("Enter your rating, choose from 1-5: ");
scanf("%d",&Rating);
printf("Enter Remarks: n");
fgets(Remarks,1000,stdin);
add_song(SongID, Title, Artist, Composer, Album, Genre, Rating, Remarks);
/*k=0;
break;
}
else if (choice1==2)
{
//update_song(song);
k=0;
break;
}
else if (choice1==3)
{
// list_songs(song);
k=0;
break;
}
else
{
k=1;
printf("That is not a valid input.n");
}
}while (k==1);*/
return 0;
}
没有遍历整个代码,但strcpy问题很明显。根据函数strcpy的定义(http://www.tutorialspoint.com/c_standard_library/c_function_strcpy.htm),它需要两个字符指针(即C中的字符串)
char *strcpy(char *dest, const char *src)
您只将char参数传递给函数add_song,它不是指针。更改函数add_song的签名,然后您应该可以使用strcpy