Welcome to HBH! If you had an account on hellboundhacker.org you will need to reset your password using the Lost Password system before you will be able to login.

Help with reading a file in C.


ghost's Avatar
0 0
	{
		char **command = 0;
		int length;//Used to keep track of the length of "command"
		FILE *f;//The file to be iterpreted
		f = fopen(argv[1],"r");
		while (1)//Infinite loop that should end when the whole file has been read
		{
			length = 0;
			while ((*command[length] != 10) && (*command[length] != 13))//Check for CrLf
			{
				command[length] = (char*) malloc(1);
				if (command[length] == NULL)//Check for valid pointer
				{
					printf("Error: can not allocate memory.");
					getchar();
					return 0;
				}
				*command[length] = getc(f);
				if (*command[length] == EOF)//Chech for end of file
				{
					return 0;
				}
				length++;
			}
			getc(f);
			printf(*command);
			//Interpret(command); Not finished
			free(*command);
		}
		getchar();
		return 0;
	}```
It's supposed to read one line at a time and send each line to the command interpreter.  When I run it, I get a message "Interpreter.exe has stopped working."  I think there is an infinite loop, but I can't find it("while(1)" should end when the end of the file is reached).  This is the first time I've ever used the C standard library, so be nice.

ghost's Avatar
0 0

I'm really not sure what you're trying to accomplish with command, but last I checked & was a bitwise operator.


ghost's Avatar
0 0
#include "stdio.h"
int main(int argc, char **argv)
{
    char **command;
    command[0] = (char*) malloc(4);
    *command[0] = 8;
    getchar();
    return 0;
}```
What's wrong with this?  I tried to simplify until I found the problem, but this *appears* correct and I'm still getting an error.

ghost's Avatar
0 0

I hope you do realize that there are different variable types for a reason. It generally makes assignment of ints to chars a bad idea. Furthermore, your compiler should provide you with enough detailed information about something like this for you to be able to tell what's wrong.

Edit: Also, unless you're storing your file in the same directory as the header files you're trying to include, I suggest you include with <> instead of "". And, the argv parameter in main is supposed to be an array last I checked.


ghost's Avatar
0 0

I don't know if you knew this but the getc() function only reads in one char at a time. http://www.cplusplus.com/reference/clibrary/cstdio/getc/

Here's how I would go about reading a file.


int main(int argc, char *argv[])
{
  if(argc != 2)
  {
    printf(&quot;&#92;nusage ./example &lt;file-path&gt;&#92;n&#92;n&quot;);
    return 0;
  }
  
  FILE *example_file;
  if((example_file = fopen(argv[1],&quot;r&quot;)) == NULL)
  {
    printf(&quot;&#92;nFile does not exist&#92;n&#92;n&quot;);
    return 0;
  }
  
  char command[100];
  while(fscanf(example_file, &quot;%s&quot;, command) != EOF)
  {
    printf(&quot;%s&#92;n&quot;, command);
    //Interpret(command);
  }
  
  fclose(example_file);
  return 0;
}```

ghost's Avatar
0 0

Nice, was waiting for you to post something here, skath. Just a warning to the OP regarding it though is to escape any spaces the path might contain, else it will not work. To OP, listen to skath here if he has something to say about C, he's more knowledgeable about it than I am for sure. However, I'd like to give you a suggestion, namely that you sit down and actually read through the basics of C and learn them well before you start fucking about with things like malloc() as you clearly need a better grasp on the basics.


ghost's Avatar
0 0

Thanks for the help. But I got to correct you("COM"), there is not inherent difference between ints and chars other than size. Although, the same is not true for the packed integer types like float and double.


ghost's Avatar
0 0

Tyler3791 wrote: Thanks for the help. But I got to correct you("COM"), there is not inherent difference between ints and chars other than size. Although, the same is not true for the packed integer types like float and double. Right… it's called implicit conversion and stuff like that can get problematic and not only with data loss.

#include &lt;stdio.h&gt;

int main()
{
char a=255;
int b=a;
printf(&quot;%u&quot;,b);
return 0;
}

Compile it, run. You might be surprised at the output.

Anyhow, could've sworn that compilers tend to give at least warnings at such direct assignment of two different types, implicit conversion or not. But, I guess I was wrong, my apologies on that note.