سؤال

I am having trouble converting code from c++ to c

هل كانت مفيدة؟

المحلول

So for starters; you can get rid of <iostream> and <fstream> includes. Replace them with <stdio.h>.

You don't need namespace std if I'm not mistaken also.

Replace strings with char*. Replace cout with printf.

For opening/reading files you can use fopen. Example:

FILE *ifp, *ofp;
char *mode = "r";
char outputFilename[] = "out.list";

ifp = fopen("in.list", mode);

if (ifp == NULL) {
  fprintf(stderr, "Can't open input file in.list!\n");
  exit(1);
}

ofp = fopen(outputFilename, "w");

if (ofp == NULL) {
  fprintf(stderr, "Can't open output file %s!\n",
          outputFilename);
  exit(1);
}

For further input/output information please look here.

نصائح أخرى

We use FILE in C instead of fstream.

FILE *fp=fopen("test.txt","r+")
if(!fp)

printf("can't open file\n");

while(!feof(fp))
{
i=0;
fscanf(fp,"%lf",&data);
printf("%lf",data);
}

If you want use String in C.You have to use Char[].

char *test = "abcd";
char  test2[10] = "abcd";

In C, you can only use char * or char [] for string. fstream is implements high-level input/output operations on file based streams for C++. In C, use FILE struct and fopen/fclose to open/close file. fgetc/fputc to read/write file.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top