c programming help

Hello i am making a level editor for my console game in c
and cant seem to get file Input/output to work
this code compiles with no warnings or errors
but when i enter the input into the value variable
the program just stops and no file data is written
i would be greatfull if somone could tell me whats wrong with my code
thanks in advance

also i know this forums is for c++ not c but i think most c++ programmers know at least a bit of c so i posted here

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
#include <stdio.h>
#include <stdlib.h>

int main()
{	
  char input[1];
  char value[1];
  
  printf("TT Level editor 0.1\n");
  printf("Enter 1 to goto the Editor\n: ");
  
  scanf("%s", input);
  if (input == "1")
  {
   FILE *fp;
   fp = fopen("Map.TT", "w");
   
   scanf("%s", value);
   
   fprintf(fp, value);
   
   fclose(fp);          
  }


  getch();
  return 0;
}
Last edited on
You are misusing char arrays. You've got some nasty memory bugs are a result.

Problem #1: Your buffers are not large enough:
1
2
  char input[1];
  char value[1];


If you have an array of 1 character... that is big enough for a string of length ZERO (because you need 1 for the null terminator). IE, it is impossible for this buffer to hold any data at all without overflowing and causing memory corruption. You need to up this to a bigger size:

1
2
  char input[100];  // now capable of holding up to 99 characters
  char value[100];  // ditto 


Problem #2: "%s" with scanf is unsafe unless you use qualifiers to specify a maximum input length.

So we have a string buffer that's capable of holding 99 characters. But what if the user inputs 100 character? Then we have memory corruption. We can make the buffer bigger... but what if the user inputs even more? There's no way we know how much the user is going to input, so there's no way to prevent them from overflowing the input buffer here.

So instead... you need to qualify %s so it doesn't exceed a maximum amount of characters:

 
  scanf( "%99s", input ); // don't exceed 99 characters 



Problem #3: You can't use the == operator to compare char buffers to string literals.

This code:
if (input == "1")
Is not comparing the contents of your 'input' variable like you think. It's actually comparing the address of your input buffer to the address of a fixed string literal in memory. These two addresses will never be the same, and therefore this if block will never execute.

Instead... when comparing string data, you need to use strcmp:

if( !strcmp(input,"1") )
thanks i use 1 for the variables as i assumed it meant you had 2 indexes in the array [0] and [1] as ["1"] and ["/0"]
Nope... it means you have 1 element. ;)

1
2
int foo[5]; // <- [0], [1], [2], [3], and [4] are valid elements
  // [5] will be OUT OF BOUNDS 


Also... if you only need 1 character... then don't use an array. Just use a single character:

 
char input;


Then you don't need %s... you can use %c instead:

 
scanf("%c",&input);


And instead of strcmp, you can use the == operator:

 
if(input == '1')  // <- note 'single quotes' not double quotes 
Last edited on
thanks!
Topic archived. No new replies allowed.