Thread: Suggestions and advices for my code.

  1. #1
    Registered User
    Join Date
    Oct 2017
    Posts
    3

    Suggestions and advices for my code.

    Hello everybody I am very new to C programming. Currently 4th week into the course. We've been given a difficult task at school (at least for me, it is....)
    Here is the thing:
    We have to write a code which will compare the argument of the command line with "DATA". This data is stored in a text file at first. We are not allowed to use functions such as fscanf, free, malloc, fopen...

    So we have to run the program like this ./program something<file.txt
    file.txt being my stdin. So far I tried to write a code for a function that would store the content of the text file into arrays but this is too hard for me and I couldn't complete it.. first I initialize integers and stuff
    then comes the loop while ((c=getchar()) !=EOF) -> something
    now I know Ive got the content of this text file in the integer c..

    Later on I would like to use these data to compare with the argv[1] letter by letter if possible.. If there are any easier ways to do this than arrays then please tell me. What are your suggestions and advices?
    This is for a project and the deadline is near and I got near to nothing so far so I am affraid.. . Any advice would be great!

  2. #2
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main (int argc, char **argv)
    {
    
    if (argc < 2)
    {
        printf("no \n");
        printf("see -->  %s , you gimme nothing, nothing I say! NOHING!!!! \n"
        "How do you expect me to work with nothing????\n", argv[1]);
        exit(1);
    }
    char c;
    
    while ((c=getchar()) !=EOF)
    {
        putchar(c);
    }
    
    return 0;
    }
    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    
    #include <string.h>
    
    
    int main (int argc, char **argv)
    {
    
    if (argc < 2)
    {
        printf("no \n");
        printf("see -->  %s , you gimme nothing, nothing I say! NOHING!!!! \n"
        "How do you expect me to work with nothing????\n", argv[1]);
        exit(1);
    }
    
    
        char *ch;
        if (fgets(argv[1], 10, stdin) == NULL)
        exit(1);
    
        ch = fgets(argv[1], 10, stdin);
        fputs(ch, stdout);
        printf("\n");
      
        return 0;
    }
    that does something too.

    maybe you'll see a difference in how to get stuff off the line, and put it back onto the line.
    Last edited by userxbw; 10-20-2017 at 01:49 PM.

  3. #3
    Registered User
    Join Date
    May 2015
    Posts
    90
    Quote Originally Posted by Rob Rob View Post
    ... file.txt being my stdin. So far I tried to write a code for a function that would store the content of the text file into arrays but this is too hard for me and I couldn't complete it.. first I initialize integers and stuff
    What did you try? What is it that you don't understand?

    Quote Originally Posted by Rob Rob View Post
    then comes the loop while ((c=getchar()) !=EOF) -> something
    now I know Ive got the content of this text file in the integer c..
    What's the content of the text file?
    Is it a simple integer? If it is say a string (a lot of chars), then there is no way it is being stored in a single char.

    Quote Originally Posted by Rob Rob View Post
    Later on I would like to use these data to compare with the argv[1] letter by letter if possible.. If there are any easier ways to do this than arrays then please tell me. What are your suggestions and advices?
    This is for a project and the deadline is near and I got near to nothing so far so I am affraid.. . Any advice would be great!
    Arrays suffice, there are functions from the string library that may help you for whatever you need.

    It is much more intuitive if you post the actual code and then explain what it is that you don't understand and what exactly you are trying to achieve.

  4. #4
    Registered User
    Join Date
    Oct 2017
    Posts
    3
    Thank you for your reply though I don't understand what you are trying to say here. To make it more clear imagine it is a different function not main -> in this function you initialize an array arr[100] for example and into this array you will store the content of the text file.
    Last edited by Rob Rob; 10-20-2017 at 01:44 PM.

  5. #5
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Rob Rob View Post
    Thank you for your reply though I don't understand what you are trying to say here. To make it more clear imagine it is a different function not main -> in this function you initialize an array arr[100] for example and into this array you will store the content of the text file.
    yep I can image that just fine, but why cannot you not write it for me first?

  6. #6
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
     
    #include <string.h>
     char *MyImaginaryFunction(char *ch);
    
     
    int main (int argc, char **argv)
    {
     
    if (argc < 2)
    {
        printf("no \n");
        printf("see -->  %s , you gimme nothing, nothing I say! NOHING!!!! \n"
        "How do you expect me to work with nothing????\n", argv[1]);
        exit(1);
    }
     
     
        char *ch;
        if (fgets(argv[1], 10, stdin) == NULL)
        exit(1);
     
      //  ch = fgets(argv[1], 10, stdin);
    
     MyImaginaryFunction( fgets(argv[1], 10, stdin) ) ;
    
    
       // fputs(ch, stdout);
    
        printf("\n");
       
        return 0;
    }
    
    char *MyImaginaryFunction(char *ch)
    {
     // code to put input into an array
    
      for (i = 0; while size of ch not equal to i; i++) {
       fill array here
    }
    
    return array?
    }
    four weeks into class and all you know how to do is tell someone how to imagine what you need to know in order to get your homework done?
    Last edited by userxbw; 10-20-2017 at 02:09 PM.

  7. #7
    Registered User
    Join Date
    Oct 2017
    Posts
    3
    I am sorry that I didn't take my time to read about how to write a code into my post. I assumed you guys would understand what I am saying . I am using the forum for the first time. if I knew how to add the code aswell, I would. However I am short on time as I mentioned before. Anyway I didn't come here to get looked down at just because I don't know something. I came here to ask something and I believe this place is made for that. Anyway thanks again for your answer. And also if you managed to be better than that in just first 4 weeks of your programming then congratulations however people are different and some cannot achieve things in such short time while others can. Hope you understand.
    Edit: I also don't know how some parts of the code should look like thats why I thought it would be better to let you guys think about the problem and maybe you could come up with something better.
    Last edited by Rob Rob; 10-20-2017 at 02:38 PM.

  8. #8
    Registered User
    Join Date
    May 2015
    Posts
    90
    Quote Originally Posted by Rob Rob View Post
    I am sorry that I didn't take my time to read about how to write a code into my post. I assumed you guys would understand what I am saying . I am using the forum for the first time. if I knew how to add the code aswell, I would. However I am short on time as I mentioned before. Anyway I didn't come here to get looked down at just because I don't know something. I came here to ask something and I believe this place is made for that. Anyway thanks again for your answer. And also if you managed to be better than that in just first 4 weeks of your programming then congratulations however people are different and some cannot achieve things in such short time while others can. Hope you understand.
    Edit: I also don't know how some parts of the code should look like thats why I thought it would be better to let you guys think about the problem and maybe you could come up with something better.
    It is more likely you will be helped if you provide code and show what you tried, and in what areas of the code you don't know what to do, or don't understand how to move on, else it becomes rather hard IMO.

    You can add code using the code tags [.code] your code here [/.code] (without the dots)

  9. #9
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Rob Rob View Post
    I am sorry that I didn't take my time to read about how to write a code into my post. I assumed you guys would understand what I am saying . I am using the forum for the first time. if I knew how to add the code as well, I would. However I am short on time as I mentioned before. Anyway I didn't come here to get looked down at just because I don't know something. I came here to ask something and I believe this place is made for that. Anyway thanks again for your answer. And also if you managed to be better than that in just first 4 weeks of your programming then congratulations however people are different and some cannot achieve things in such short time while others can. Hope you understand.
    Edit: I also don't know how some parts of the code should look like that's why I thought it would be better to let you guys think about the problem and maybe you could come up with something better.
    I consider myself the idiot in here. or the less learned, because I am self taught. but as stated, it helps to see what you do know besides
    first I initialize integers and stuff
    then comes the loop while ((c=getchar()) !=EOF) -> something
    now I know Ive got the content of this text file in the integer c..
    which while ((c=getchar()) !=EOF) can be gotten off of google.

    nothing about a function or putting things into array then checking data within a file you cannot open using fopen to compare it to something gotten off the line. that does not help me at all.

    if someone did give that all of what you needed in a main and you still could not put that into a function where does that leave you?

    you do know main is a function as well yes?

    don't let that scare you away from trying, and putting some best effort code in here then asking for help.
    Last edited by userxbw; 10-20-2017 at 03:27 PM.

  10. #10
    Banned
    Join Date
    Aug 2017
    Posts
    861
    now put that into a function, practice your arrays and function creation with return values
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    // format ./program_name < filename
    // maybe Linux specific
    int main(void)
    {
      char str[256];
      
      while (fgets(str, 100, stdin) != NULL){
               printf("%s\n", str);
    }
    
      return 0;
    }
    I hope you learn something from this.

    what does it do you ask? opens a file and prints it out to the screen without using fopen.
    don't for get to you your arrow < redirection
    fgets
    Last edited by userxbw; 10-20-2017 at 05:01 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some advices about c++ ,not just about the code
    By Foxefde in forum C++ Programming
    Replies: 20
    Last Post: 05-16-2013, 10:00 PM
  2. Need some advices with tic-tac.My code is to long ...
    By Foxefde in forum C++ Programming
    Replies: 9
    Last Post: 04-01-2013, 09:49 AM
  3. Suggestions for this code? and 1 question....
    By Huskar in forum C Programming
    Replies: 6
    Last Post: 03-31-2009, 09:24 AM
  4. some suggestions to speed up code
    By -EquinoX- in forum C Programming
    Replies: 19
    Last Post: 05-02-2008, 11:03 AM
  5. homework code/looking for suggestions
    By m.albert in forum C++ Programming
    Replies: 3
    Last Post: 03-03-2003, 03:50 PM

Tags for this Thread