Read and write

So I am self learning read/write in c I visited http://www.cplusplus.com/reference/cstdio/fread/

I am trying to prompt the user to type something like "my name is keith"
then it will edit the file and print out "my name is keith"
The code below is my attempt. I got the following error.
readwrite.c: In function ‘main’:
readwrite.c:8:8: error: invalid suffix "Size" on integer constant
long 1Size;
^
readwrite.c:8:8: error: expected identifier or ‘(’ before numeric constant
readwrite.c:12:52: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
if (pFile == NULL) {fputs ("File error",stderr); exit (1);}
^
readwrite.c:20:3: error: invalid suffix "Size" on integer constant
1Size = ftell (pFile);
^
readwrite.c:23:20: warning: incompatible implicit declaration of built-in function ‘malloc’ [enabled by default]
buffer = (char*) malloc(sizeof (char) *1Size);
^
readwrite.c:23:42: error: invalid suffix "Size" on integer constant
buffer = (char*) malloc(sizeof (char) *1Size);
^
readwrite.c:24:55: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}
^
readwrite.c:27:3: error: ‘result’ undeclared (first use in this function)
result = fread (buffer,1,1Size,pFile);
^
readwrite.c:27:3: note: each undeclared identifier is reported only once for each function it appears in
readwrite.c:27:28: error: invalid suffix "Size" on integer constant
result = fread (buffer,1,1Size,pFile);
^
readwrite.c:28:17: error: invalid suffix "Size" on integer constant
if (result != 1Size) {fputs ("Reading error", stderr); exit (3);}
^
readwrite.c:28:58: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
if (result != 1Size) {fputs ("Reading error", stderr); exit (3);}
^
readwrite.c:34:3: warning: incompatible implicit declaration of built-in function ‘free’ [enabled by default]
free (buffer);
^

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/* fscanf example */
#include <stdio.h>

int main ()
{
  char word[80];
  char * buffer;
  long 1Size;
  FILE * pFile;

  pFile = fopen ("myfile.txt","rb+");
  if (pFile == NULL) {fputs ("File error",stderr); exit (1);}
  //fprintf (pFile, "%f %s", 3.1416, "PI");  
  //fscanf (pFile, "%f", &f);
  printf("enter your string: ");
  scanf("%s", word);
  fprintf (pFile, "%s", word);

  fseek (pFile, 0, SEEK_END);
  1Size = ftell (pFile);
  rewind (pFile);

  buffer = (char*) malloc(sizeof (char) *1Size);
  if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}


  result = fread (buffer,1,1Size,pFile);
  if (result != 1Size) {fputs ("Reading error", stderr); exit (3);}

  printf ("I have reads: %s \n",buffer);

  //fscanf (pFile, "%s", buffer);
  fclose (pFile);
  free (buffer);
  return 0;
}
You cannot start names with a number. The compiler will attempt to interpret it as a number with some kind of suffix, like
 
float f = 1.0f; //f here denotes that 1.0 should be of float type 
^ what do you mean, everything is stored as a string in my code.
 
long 1Size; //<-- Variable name cannot start with a number 
I change it to char 1Size; I still get the same problem.
I meant the name you gave your variable. Try using:
 
long Size1;
Topic archived. No new replies allowed.