Follow us on Twitter!
Don't judge the unknown - Grindordie
Sunday, April 20, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 21
Guests Online: 21
Members Online: 0

Registered Members: 82843
Newest Member: hx47
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

Help with reading a file in C.


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 23-12-09 07:30
Code
   if (argc > 1)
   {
      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.

Edited by on 23-12-09 11:37
Author

RE: Help with reading a file in C.


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 23-12-09 10:35
I'm really not sure what you're trying to accomplish with command, but last I checked & was a bitwise operator.


Author

RE: Help with reading a file in C.


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 23-12-09 11:53
Code
#include "stdlib.h"
#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.
Author

RE: Help with reading a file in C.


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 23-12-09 11:57
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.




Edited by on 23-12-09 12:03
Author

RE: Help with reading a file in C.


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 23-12-09 17:30
I don't know if you knew this but the getc() function only reads in one char at a time. http://www.cplusp. . .tdio/getc/

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

Code
#include <stdio.h>

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





Author

RE: Help with reading a file in C.


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 23-12-09 19:24
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.


Author

RE: Help with reading a file in C.


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 24-12-09 15:34
Thanks for the help. But I got to correct you("COM"Wink, 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.
Author

RE: Help with reading a file in C.


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 24-12-09 16:42
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.
Code

#include <stdio.h>

int main()
{
char a=255;
int b=a;
printf("%u",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.