检测文件是否包含整数以外的字符

时间:2017-04-22 12:00:10

标签: c

int main(int argc, char *argv[])
{/*Please ignore extra declarations*/
    FILE *fp;
    int integers[100];
    int value;
    int i = -1;
    char *addr1;
    if ((fp = fopen (argv[1], "r")) == NULL)
    {
        printf("cant open");
        return 1;
    }
    while (!feof(fp) && fscanf(fp, "%d", &value) && ++i < 100)
        integers[i] = value;

    fclose (fp);
    int k;
    k = i;

    int arrlen, processes, j, *pint, number;
    float p;
    arrlen = k;

    printf("Size of array is %d\nThe array is:\n", arrlen);
    for(i = 0; i < k; i++)
    {
        printf("%d\t", integers[i]);
    }
    return 0;
}

我必须从文件读入整数数组。此外,如果文件包含除整数之外的任何其他字符,则应报告错误。我能够完成上面发布的第一部分。请帮我解决第二部分。如果文件只包含整数,则将其存储在array.PS中:这只是某个程序的一部分。我必须稍后在程序中处理整数数组。

1 个答案:

答案 0 :(得分:2)

阅读输入后,如果fscanf读取字符,则EOF.将返回0,这意味着在while

之前它不会读取

fp之后但在关闭if(!(feof(fp) || i==100)) { printf("Contains character"); return 1; }

之前使用此功能
public static Bitmap DrawBorder(Bitmap bitmap)
{
    float ratio = 0.97f;
    int width = bitmap.Width;
    int height = bitmap.Height;
    Bitmap outputBitmap = Bitmap.CreateBitmap(width, height, Config.Argb8888);

Path path = new Path();
Canvas canvas = new Canvas(outputBitmap);

//Draw Border
Paint paint = new Paint();
paint.AntiAlias = true;
paint.SetStyle(Paint.Style.Fill);
paint.SetXfermode(null);
//   paint.SetStyle(Paint.Style.Stroke);
paint.Color = Color.White;
//   paint.StrokeWidth = 6;
canvas.DrawCircle((float)width / 2, (float)height / 2, (float)Math.Min(width / 2, (height / 2)), paint);

//Draw Picture 
path.AddCircle(
          (float)(width / 2)
        , (float)(height / 2)
        , (float)Math.Min(width / 2, (height / 2)) * ratio
        , Path.Direction.Ccw);

canvas.ClipPath(path);
canvas.DrawBitmap(bitmap, 0, 0, paint);



return outputBitmap;