How to add characters to reach the maximum size of a char[]
I have the following part of code :
for(int i=0;i<n;i++)
{
printf("Student %d\n",i+1);
printf("Enter name 开发者_高级运维: ");
scanf("%s",&(student+i)->name);
fflush(stdin);
lengthName = strlen((student+i)->name);
while(lengthName !='\0')
{
}}
when the length is shorter than 10, it will add hyphens until reaching the maximum size. Ex : John =>> 6 hyphens will be added
I know how to do it in csharp but can't figure it out in c. Could some of you give me some lights please?
PS : Oh yes the variable name is char name[10+1] and it a part of the structure called student.
This is so simple that it seems like I must be missing something.
lengthName = strlen(student[i].name);
while (lengthName < 10)
student[i].name[lengthName++] = '-';
student[i].name[lengthName] = '\0';
Perhaps you are confused by C#'s (presumed) possession of a first-class string type? No such thing in C, only bare arrays of characters; which is at once tedious (you have to do all the memory management yourself) and liberating (as you see above).
while(lengthName < 10)
{
student[i]->name[lengthName++] = '-'; // add this line
}
student[i]->name[lengthName] = 0; // and this line
How about this?
// Pre-fills with hyphens.
memset(student[i].name, '-', sizeof(student[i].name) - 1);
scanf("%10s", student[i].name);
Modified version of @kotlinski's idea:
#define NAME_SIZE 10
fgets(student[i].name, NAME_SIZE, stdin);
lengthName = strlen(student[i].name);
if (lengthName > 0 && student[i].name[lengthName-1] == '\n') {
--lengthName;
}
if (lengthName < NAME_SIZE) {
memset(student[i].name + lengthName, '-', NAME_SIZE - lengthName);
student[i].name[NAME_SIZE] = '\0';
}
Your char name[10 + 1];
variable is obviously 11 bytes long. Adding anything more than 11 bytes will cause a buffer overflow.
This is where the buffer overflow might happen:
scanf("%s",&(student+i)->name);
Your hyphens feature can come after you fixed this possible buffer overflow issue.
Fixing the buffer overflow
One easy way to fix this sort of problem is to have scanf itself limit the number of chars to read:
// read only 10 chars, since `name` can only
// hold 10 chars, plus the extra NULL char
scanf("%10s", &(student+i)->name);
Pre-filling with hyphens
Finally, after you took care of your memory issue, you can go ahead and pre-fill your string before overwriting it with the actual student data.
// pre-fill your data with hypens.
memset((student+i)->name, '-', sizeof((student+i)->name) - 1);
// read only 10 chars, since `name` can only
// hold 10 chars, plus the extra NULL char
scanf("%10s", &(student+i)->name);
Use fgets()
to read the input, with the understanding that it will save the newline character to the buffer if there's room; you'll have to add some special handling for that.
Note that the behavior of fflush()
is not defined for input streams; you cannot clear unwanted input by calling fflush(stdin);
.
Then it's a matter of setting the remaining characters to hyphens.
if (fgets(students[i].name, sizeof students[i].name, stdin) != NULL)
{
size_t max = sizeof students[i].name - 1;
size_t len = 0;
char *newline = strchr(students[i].name, '\n');
if (newline)
*newline = 0; // clear out newline
else
{
while (fgetc(stdin) != '\n') // clear out remaining input;
// name potentially truncated
;
}
// pad remainder with hyphens
len = strlen(students[i].name);
while (len < max)
students[i].name[len++] = '-';
students[i].name[max] = 0;
}
Try the following:
while (lengthName < 10)
{
(student+i)->name[lengthName++] = '-';
}
精彩评论