Jump to content

SgtBot

Member
  • Posts

    77
  • Joined

  • Last visited

Reputation Activity

  1. Informative
    SgtBot reacted to cluelessgenius in Smart home programming applications with Raspberry Pi   
    not sure what you mean by effective. wol is a simple packet like any other but with a specific content and target. id start by looking into what a wol packet looks like and youll understand.
    im sure there are but they are not neccessarily neccesary
    push2run , google it. does exactly that. triggered by a pushbullet message. can even configure over ifttt directly so no pi neccessary. but i would try looking into wether you can just send windows to sleep from another machine. with shell commands. i know you can from one windows machine to another but not sure about a pi. 
  2. Like
    SgtBot reacted to Sauron in Smart home programming applications with Raspberry Pi   
    You can use wol and embed it in a script. You do need to enable WoL functionality on your target computer. There's an article about this on the Arch wiki, it should apply to Raspbian too.
  3. Like
    SgtBot reacted to vorticalbox in Smart home programming applications with Raspberry Pi   
    From what you have described the windows machine power up and shut down trigger the same on the pie.
     
    In which case just make windows turn on the pi after it boots and turn it off before it shuts down.
  4. Informative
    SgtBot reacted to Hans Christian | Teri in Smart home programming applications with Raspberry Pi   
    Might want to take a look at this for a Python wake on lan script.
  5. Informative
    SgtBot reacted to Egad in Python File Sorter Help   
    Use os.rename or shutil.move to actually move the file.  Those commands will handle white space escaping for you.
     
    You might also find filename, file_extension = os.path.splitext a useful command.  Because splitting on period is going to fail on "my.file.has.periods.docx"
  6. Informative
    SgtBot reacted to Hrillo666 in Help with C program that prints contents of a file   
    Hi sorry to barge in - I did this exercise back at uni, only for the teacher to come back and say that "cat and less uses memory mapped files, so you were all wrong...". So google mmap and munmap if you want extra credit 
  7. Informative
    SgtBot reacted to Sauron in Help with C program that prints contents of a file   
    That would be because fopen only needs the file path and the opening mode (read the man page!).
     
    I see @Unimportant already corrected you there though.
  8. Informative
    SgtBot reacted to Unimportant in Help with C program that prints contents of a file   
    The simplest way is to read the file character by character and print each character out until EOF is reached:
    #include <stdio.h> int main(int argc, char *argv[]) { if (argc != 2) { printf("Incorrect amount of command line parameters!\n"); return 1; } FILE* fptr = fopen(argv[1], "r"); if (!fptr) { printf("Error opening file!\n"); return 2; } while (1) { int ch = getc(fptr); if (ch == EOF) { return 0; } putchar(ch); } }  
  9. Informative
    SgtBot reacted to Unimportant in Help with C program that prints contents of a file   
    When you compile with the flag "-Wimplicit-function-declaration" you should get:
    Which means the compiler has not seen a prototype for the function "open" yet (put simply: it does not exist) and has implicitly declared one as:
    int open(); Which is where your first error comes from:
    The implicitly declared open function returns a "int" which you then try to assign to a "FILE" pointer.
     
    You should use "fopen" instead. The reason "fopen" does not work is because you're giving it wrong parameters.
     
    Your second error:
    Is due to the fact that you're passing the result of the "readfile" function, which is of type "int" to "printf", which expects a "char" pointer, pointing to the string to print.
     
    Here's an example of what I think you're trying to do:
    #include <stdio.h> #include <stdlib.h> int ReadFile(const char *infile) { int num; FILE *fptr; fptr = fopen(infile, "r"); if (!fptr) { printf("Error opening file!\n"); exit(1); //abort program with nonzero return code, indicating error. } /* fscanf returns the number of items successfully extracted, which should be 1... because we ask to read a single integer. */ if (fscanf(fptr, "%d", &num) < 1) { printf("Failed to read a valid number!\n"); exit(2); } return num; } int main(int argc, char *argv[]) { if (argc != 2) { printf("Incorrect amount of command line parameters!\n"); return 3; } printf("Number read from file: %d\n", ReadFile(argv[1])); return 0; } ( Note that this example is simply a modification of your code to make it work. The whole program structure is questionable tough - Opening and reading the file should be handled by separate functions, removing the need for "exit", for example ).
  10. Informative
    SgtBot reacted to mariushm in Help with C program that prints contents of a file   
    In your code, if the file can not be opened you're printing the error message, but you're still trying to read and return a number afterwards.
    You should have an else { } and put the stuff after there.
    A lot of programmers prefer to put the "meat" of the if at the start and something smaller (error handling) at the bottom in the else. So it would probably be more readable, easier to understand to write it like this   if the file handle is NOT null then do lots of things  ELSE  say error message
     
    here's a rewrite attempt of that function, note I'm writing it in this post editor, not checking and compiling it, may contain errors
     
    int readFile(char *infile) { int num; FILE *fptr; fptr = open("/home/students/hansong5/crypter/%c", infile, "r"); if (fptr !== NULL) { fscanf(fptr, "%d", &num); fclose(fptr); } else { printf("Error opening file"); // you HAVE to return something, best to initialize num with a value i guess num = -1; } return num; }  
    I'd suggest getting used NOT having accolades  { } by themselves on single lines ... you may think it makes your code more readable but it really doesn't ... makes keeping track of elses and where each if ends harder (if you have an if within an if or something like that)
  11. Informative
    SgtBot reacted to Sauron in Help with C program that prints contents of a file   
    First of all, that's not how you call printf. You need to specify a format string. In your case, the correct call would be:
    printf("%d\n", readFile(argv[1])); you can find this information in the man page for printf
     
    secondly, if you want to declare the file as a pointer, you should use fopen and not open.
     
    The rest looks fine to me, but the compiler will be the judge of that
  12. Informative
    SgtBot reacted to reniat in scanf() function help in C   
    So you can't actually pass an array as a value in C/C++ (you can pass structs and classes as values, but arrays are not those things). When you take in an array as a function parameter, you are actually passing the pointer to the location in memory of the first element in said array.
     
    So your function declaration here
    int getString(int maxCharacters, char characterArray[100]) is actually identical to
    int getString(int maxCharacters, char *characterArray) so when you try to get the address with
    scanf(" %s", &characterArray); you are attempting to tell scanf to put the user input into the address of where the pointer to where characterArray is stored on the stack, not where your characterArray is stored. In other words, you're one address too deep. Just remove the & and this should work fine.
     
    The reason you were getting a segfault was because scanf was trying to write your char * to the char * * at location &characterArray.
  13. Informative
    SgtBot reacted to zhick in C if statement help   
    Maybe to expand on this a little bit:
    Yes, this is a very simple mistake, but mistakes of this kind are quite common and stem from doing "the same thing" at two different points, which makes it easy to mess up.
    In this case you could restructure your program to make it so the input is only always read at one point:
    Instead of
    int user_input = 0; // ... printf("Enter an integer:\n"); scanf("%d", &user_input); while(user_input != 0) { // ... printf("Enter an integer:\n"); scanf("%d", &user_input); // ... } You could just have this as
    do { int user_input = 0; printf("Enter an integer:\n"); scanf("%d", &user_input); if (user_input == 0) exit; // .. } while (1) This way it's always clear when input is read (only ever at the beginning of the loop) and when it's processed.
  14. Like
    SgtBot reacted to link-omthsed in C if statement help   
    yep you got it  other persons comment was better as you learnt from it i'm not a good teacher i just like to answer.
  15. Like
    SgtBot reacted to zhick in C if statement help   
    Exactly.
  16. Informative
    SgtBot reacted to zhick in C if statement help   
    Again:
    To be more precise: Which number are you using to decide whether a number is negative or positive?
  17. Informative
    SgtBot reacted to zhick in C if statement help   
    Yeah, maybe I could've worded that better. What's the value of the variable you're comparing to zero (don't worry, that part is correct) at that point in the program?
    Btw, I don't mean to lead you on, so if you'd prefer me to just tell you the solution just say so. I just think it's better to point someone in the right direction and let them figure out the rest of the way themselves.
  18. Informative
    SgtBot reacted to 2FA in C if statement help   
    Look here:
    printf("Enter an integer:\n"); scanf("%d", &user_input); while(user_input != 0) { nonzero_count++; total_sum = total_sum + user_input; average = (float)total_sum/(float)nonzero_count; if(user_input > largest) largest = user_input; if(user_input < smallest) smallest = user_input; printf("Enter an integer:\n"); scanf("%d", &user_input);  
  19. Informative
    SgtBot reacted to link-omthsed in C if statement help   
    its because you are setting the user_input just before the dowhile then resetting it again before you hit your IF statment. this means the first entered amount will do the first part of the do while but never get to your if statments.
     
    printf("Enter an integer:\n"); scanf("%d", &user_input); while(user_input != 0) { nonzero_count++; total_sum = total_sum + user_input; average = (float)total_sum/(float)nonzero_count; if(user_input > largest) largest = user_input; if(user_input < smallest) smallest = user_input; if(user_input > 0) { positive_count++; positive_sum = positive_sum + user_input; average_positive = (float)positive_sum/(float)positive_count; if(user_input > largest_positive) { largest_positive = user_input; } if(user_input < smallest_positive) { smallest_positive = user_input; } } else if(user_input < 0) { negative_count++; negative_sum = negative_sum + user_input; average_negative = (float)negative_sum/(float)negative_count; if(user_input > largest_negative) { largest_negative = user_input; } if(user_input < smallest_negative) { smallest_negative = user_input; } } printf("Enter an integer:\n"); scanf("%d", &user_input); }
  20. Informative
    SgtBot reacted to zhick in C if statement help   
    Your problem is not that you're not counting negative numbers, but you're not counting the last number entered:
    Enter an integer: 1 Enter an integer: 1 Enter an integer: 0 Overall: Count is 2, sum is 2, maximum is 1, minimum is 0, average is 1.000000 Positives: Count is 1, sum is 1, maximum is 1, minimum is 1, average is 1.000000 Negatives: Count is 0, sum is 0, maximum is -65535, minimum is 65535, average is 0.000000 Take a close look at when you do the counting.
  21. Informative
    SgtBot reacted to grimreeper132 in C if statement help   
    move the 
    nonzero_count++; to the end of the while loop as your adding 1 to it immediately, so e.g you start nonzero_count at 0 outside the while loop the first number you will be testing from it will be 1 as your immediately adding that 1 to it. 
  22. Informative
    SgtBot reacted to TheBean in Loop logic question for C   
    you could change that to "printf("%d *", potential_factor);
    but that would also add a * at the end of the last factor.... maybe set a if statement saying if it is the last factor, : printf("%d ", potential_factor);
  23. Informative
    SgtBot reacted to Mira Yurizaki in Loop logic question for C   
    You could either:
    Have it evaluate the first factor, then in the loop the first thing it does is print an asterisk. Have the loop check if the exit condition fails and if it does, print an asterisk. I prefer the first one over the second if only because the algorithm is not having to evaluate another if-statement in the loop. You just have to remember to adjust the initial value of potential_factor
  24. Informative
    SgtBot reacted to colonel_mortis in C prime number program help   
    Your fixed code just adds a workaround for 3, but it doesn't solve the underlying issue. For example, it still thinks that 25 is prime, because it's not divisible by 2.
    0 and 1 are indeed the C analogues of false and true, and using them as false/true to track whether the number has any divisors should work.
  25. Informative
    SgtBot reacted to elpiop in C prime number program help   
    edit: Seems you fixed it in your post at the same time I commented
     
     
×