English 中文(简体)
- C - 切分过失
原标题:C - Segmentation fault
  • 时间:2012-05-21 21:45:39
  •  标签:
  • c

这是我的小片段 产生断裂断裂断层

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int checkTiime(char time[]);

int main(int argc, char** argv) {

    char time1[6];
    char time2[6];

    printf("Tempo 1 [hh:mm]: ");
    fgets(time1, sizeof time1, stdin);
    printf("Tempo 2 [hh:mm]: ");
    fgets(time2, sizeof time2, stdin);

    printf("Checktime: %d", checkTime(time1));

    return (EXIT_SUCCESS);
}

int checkTime(char time[]){

    long hours = 0;
    long minutes = 0;

    if(time[2] ==  : ) {

        if(isdigit(time[3]) && isdigit(time[4]) && isdigit(time[0]) && isdigit(time[1])) {
            hours = strtol(time[0], NULL, 10) + strtol(time[1], NULL, 10);
            minutes = strtol(time[3], NULL, 10) + strtol(time[4], NULL, 10);

            if((hours >= 0 && hours <= 23) && (minutes >= 0 && minutes <= 59)){

                return 0;

            } else {

                return 1;

            }

        } else {

            return 1;

        } 

    } else {

        return 1;

    }

}

谁能帮我 我真的不知道为什么给我带来麻烦

我还注意到,例如,当我输入“12: 34”时,它要求我输入第二个输入,但是当我写“12: 34”时,然后我用后空格删除“34”,再输入“34”,它写第二个打印页f,但不允许我输入第二个输入以及程序出口。

<强度>个人评论:

I noticed that it s better to use gets() function to input strings beacause it doesn t count the character.

最佳回答

Be careful when using fgets. Your calls require it to read in at most 6 characters before it hits either a newline or EOF. fgets considers the newline character to be a valid character and stores it too, if one such is encountered. Now, in your case, the last character is not a NULL as it should in C-style strings but a . You need to replace it by a NULL before you can pass it to strtol which expects a C-style NULL terminated string. So:

if (fgets(time1, sizeof time1, stdin) != NULL) {
    time1[ sizeof time1 - 1 ] = NULL;
}

或者,更好的是:

if (fgets(time1, sizeof time1 - 1, stdin) != NULL) {
    // hurray we have the time!
}

或者,您可以拥有您自己的新线安全 fgets 包装纸 :

char *chomp(char *buf, size_t sz, FILE *fp, size_t *outsz) {
   char *p = fgets(buf, sz - 1, fp) ; /* make sure there is room for terminating NULL */
   if (p != NULL) {
      char *nl = strchr(buf,  
 );
      if (nl) *nl = NULL; /* replace trailing newline */
      *outsz = strlen(buf); /* recompute */
   }
   return p;
}
问题回答

暂无回答




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签