Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Open the file but do not save the args

Open the file but do not save the args

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionperformance
13 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H honor3us

    #include #include #include int main(int *op[]) {
    char n;
    int total;
    int ct = 0, oper1 = 0, oper2 = 0, oper3 = 0, i = 0;
    FILE *arq1;

    arq1 = fopen("Maq1.log", "r"); //open file
    while (!feof(arq1)) {
    n = fgetc(arq1);
    if (n == "\n") { //count lines
    ct++;
    }
    }
    op = (int*) calloc(ct, sizeof(int)); //alloc memory
    if(op==0) {
    printf("Não houve memoria alocada!\n");
    return 0;
    }
    for(i=0;!feof(arq1);i++) {
    fscanf(",,,%d,", &op[ct]); //I think this is the error
    }
    for(i=0;i<=ct;i++) { //do the count
    if(op[ct] == 1){
    oper1++;
    }
    if(op[ct] == 2){
    oper2++;
    }
    if(op[ct] == 3){
    oper3++;
    }
    }
    total = oper1 + oper2 + oper3;
    printf("Operacao 1: %d \n Operacao 2: %d \n Operacao 3: %d \n Total: %d \n", oper1, oper2, oper3, total); //and print!
    fclose(arq1);
    return 0;
    }

    Can you guys help me on this Code? I have no idea of what is happening, but when I run the code he prints

    Operacao 1: 0
    Operacao 2: 0
    Operacao 3: 0
    Total: 0

    Sorry for the bad english!

    J Offline
    J Offline
    jeron1
    wrote on last edited by
    #2

    Have you tried stepping through the code using a debugger?

    "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

    H 1 Reply Last reply
    0
    • H honor3us

      #include #include #include int main(int *op[]) {
      char n;
      int total;
      int ct = 0, oper1 = 0, oper2 = 0, oper3 = 0, i = 0;
      FILE *arq1;

      arq1 = fopen("Maq1.log", "r"); //open file
      while (!feof(arq1)) {
      n = fgetc(arq1);
      if (n == "\n") { //count lines
      ct++;
      }
      }
      op = (int*) calloc(ct, sizeof(int)); //alloc memory
      if(op==0) {
      printf("Não houve memoria alocada!\n");
      return 0;
      }
      for(i=0;!feof(arq1);i++) {
      fscanf(",,,%d,", &op[ct]); //I think this is the error
      }
      for(i=0;i<=ct;i++) { //do the count
      if(op[ct] == 1){
      oper1++;
      }
      if(op[ct] == 2){
      oper2++;
      }
      if(op[ct] == 3){
      oper3++;
      }
      }
      total = oper1 + oper2 + oper3;
      printf("Operacao 1: %d \n Operacao 2: %d \n Operacao 3: %d \n Total: %d \n", oper1, oper2, oper3, total); //and print!
      fclose(arq1);
      return 0;
      }

      Can you guys help me on this Code? I have no idea of what is happening, but when I run the code he prints

      Operacao 1: 0
      Operacao 2: 0
      Operacao 3: 0
      Total: 0

      Sorry for the bad english!

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #3

      The three if() conditions in your for() loop are always looking at op[ct] rather than op[i]. I do not think that is your intent. ;) A date with the debugger, as jeron1 suggested, would have unveiled that.

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

      H 1 Reply Last reply
      0
      • H honor3us

        #include #include #include int main(int *op[]) {
        char n;
        int total;
        int ct = 0, oper1 = 0, oper2 = 0, oper3 = 0, i = 0;
        FILE *arq1;

        arq1 = fopen("Maq1.log", "r"); //open file
        while (!feof(arq1)) {
        n = fgetc(arq1);
        if (n == "\n") { //count lines
        ct++;
        }
        }
        op = (int*) calloc(ct, sizeof(int)); //alloc memory
        if(op==0) {
        printf("Não houve memoria alocada!\n");
        return 0;
        }
        for(i=0;!feof(arq1);i++) {
        fscanf(",,,%d,", &op[ct]); //I think this is the error
        }
        for(i=0;i<=ct;i++) { //do the count
        if(op[ct] == 1){
        oper1++;
        }
        if(op[ct] == 2){
        oper2++;
        }
        if(op[ct] == 3){
        oper3++;
        }
        }
        total = oper1 + oper2 + oper3;
        printf("Operacao 1: %d \n Operacao 2: %d \n Operacao 3: %d \n Total: %d \n", oper1, oper2, oper3, total); //and print!
        fclose(arq1);
        return 0;
        }

        Can you guys help me on this Code? I have no idea of what is happening, but when I run the code he prints

        Operacao 1: 0
        Operacao 2: 0
        Operacao 3: 0
        Total: 0

        Sorry for the bad english!

        K Offline
        K Offline
        k5054
        wrote on last edited by
        #4

        This has nothing to do with the solution to your problem, as DavidCrow has already given you a clue. I'm just wondering if your compiler isn't giving you any warnings about your definition of main(). int main(int *op[]) definitely isn't one of the normal ways to define main() Normally it would be one of

        int main()
        int main(void)
        int main(int argc, char *argv[])
        int main(int argc, char **argv)

        On some systems you might also be able to use int main(int argc, char *arg[], char *envp[]) Note that as a function parameter char *arg[] and char **arg are equivalent. You also have a type issues with op. It's declared as int *op[], which is an array of pointer to int, but you are using it as a pointer to int. What you probably want to do is:

        int main(void)
        {
        ...
        int \*op;
        op = (int\*)calloc(ct, sizeof *op);
        ...
        }

        If you're wondering why I used sizeof *op as the second argument to calloc(), consider what might happen if you decide you want to change op from an int to a long, and what might happen if you forget that you need change the call to calloc() too.

        H 1 Reply Last reply
        0
        • J jeron1

          Have you tried stepping through the code using a debugger?

          "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

          H Offline
          H Offline
          honor3us
          wrote on last edited by
          #5

          gcc -c "trabalho.c" -o "trabalho.o"
          trabalho.c: In function ‘main’:
          trabalho.c:22:11: warning: comparison between pointer and integer
          if (n == "\n") { //count lines
          ^
          trabalho.c:35:5: warning: format ‘%d’ expects argument of type ‘int *’, but argument 3 has type ‘int’ [-Wformat=]
          fscanf(arq1, ",,,%d,", op[ct]); //I think this is the error
          ^
          g++ -o "trabalho.o"
          Process terminated with status 0 (0 minute(s), 0 second(s))
          0 error(s), 2 warning(s) (0 minute(s), 0 second(s))

          Checking for existence: /home//
          Executing: xterm -T 'dest" (in origem)
          Process terminated with status 0 (0 minute(s), 2 second(s))

          When I compile the .c archive, appears just those 2 warnings

          1 Reply Last reply
          0
          • D David Crow

            The three if() conditions in your for() loop are always looking at op[ct] rather than op[i]. I do not think that is your intent. ;) A date with the debugger, as jeron1 suggested, would have unveiled that.

            "One man's wage rise is another man's price increase." - Harold Wilson

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

            H Offline
            H Offline
            honor3us
            wrote on last edited by
            #6

            Thank you! I didn't see that error of logic... --' But still giving me the same answer :/ I think the error is when he count the lines on

            char n;
            while (!feof(arq1)) {
            n = fgetc(arq1);
            if (n == "\n") { //count lines
            ct++;
            }
            }

            warning: assignment makes pointer from integer without a cast and...

            for(i=0;!feof(arq1);i++) {
            fscanf(arq1, ",,,%d,", op[i]); //I think this is the error
            }

            format ‘%d’ expects argument of type ‘int *’, but argument 3 has type ‘int’ [-Wformat=]|

            D 1 Reply Last reply
            0
            • K k5054

              This has nothing to do with the solution to your problem, as DavidCrow has already given you a clue. I'm just wondering if your compiler isn't giving you any warnings about your definition of main(). int main(int *op[]) definitely isn't one of the normal ways to define main() Normally it would be one of

              int main()
              int main(void)
              int main(int argc, char *argv[])
              int main(int argc, char **argv)

              On some systems you might also be able to use int main(int argc, char *arg[], char *envp[]) Note that as a function parameter char *arg[] and char **arg are equivalent. You also have a type issues with op. It's declared as int *op[], which is an array of pointer to int, but you are using it as a pointer to int. What you probably want to do is:

              int main(void)
              {
              ...
              int \*op;
              op = (int\*)calloc(ct, sizeof *op);
              ...
              }

              If you're wondering why I used sizeof *op as the second argument to calloc(), consider what might happen if you decide you want to change op from an int to a long, and what might happen if you forget that you need change the call to calloc() too.

              H Offline
              H Offline
              honor3us
              wrote on last edited by
              #7

              Thank you! You help me a lot! But the compiler still having problem to count lines and fscanf the data I need, I don't have a clue what is going on--'

              K 1 Reply Last reply
              0
              • H honor3us

                Thank you! I didn't see that error of logic... --' But still giving me the same answer :/ I think the error is when he count the lines on

                char n;
                while (!feof(arq1)) {
                n = fgetc(arq1);
                if (n == "\n") { //count lines
                ct++;
                }
                }

                warning: assignment makes pointer from integer without a cast and...

                for(i=0;!feof(arq1);i++) {
                fscanf(arq1, ",,,%d,", op[i]); //I think this is the error
                }

                format ‘%d’ expects argument of type ‘int *’, but argument 3 has type ‘int’ [-Wformat=]|

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #8

                honor3us wrote:

                if (n == "\n") { //count lines

                Why are you not using:

                if (n == '\n')

                "One man's wage rise is another man's price increase." - Harold Wilson

                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                H 1 Reply Last reply
                0
                • D David Crow

                  honor3us wrote:

                  if (n == "\n") { //count lines

                  Why are you not using:

                  if (n == '\n')

                  "One man's wage rise is another man's price increase." - Harold Wilson

                  "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                  "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                  H Offline
                  H Offline
                  honor3us
                  wrote on last edited by
                  #9

                  Another error, sorry :/ I changed to 'n' and &op[i] and all the warnings are gonne! :) The programs runs perfectly but all the values of vector op[] are == 0. ex:

                  printf("%d\n%d", op[4], op[5]);

                  The answer still

                  0
                  0

                  I'm really thankful! :D

                  D 1 Reply Last reply
                  0
                  • H honor3us

                    Another error, sorry :/ I changed to 'n' and &op[i] and all the warnings are gonne! :) The programs runs perfectly but all the values of vector op[] are == 0. ex:

                    printf("%d\n%d", op[4], op[5]);

                    The answer still

                    0
                    0

                    I'm really thankful! :D

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #10

                    See here.

                    "One man's wage rise is another man's price increase." - Harold Wilson

                    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                    "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                    1 Reply Last reply
                    0
                    • H honor3us

                      Thank you! You help me a lot! But the compiler still having problem to count lines and fscanf the data I need, I don't have a clue what is going on--'

                      K Offline
                      K Offline
                      k5054
                      wrote on last edited by
                      #11

                      You might want to look at this section of the code ... there's definitely an issue here:

                      for( i=0; i<=ct; i++) { //do the count
                      if(op[ct] == 1){
                      oper1++;
                      }

                      Also, you are accessing past the end of the array *op[]* -- what should the end condition for the loop be?

                      1 Reply Last reply
                      0
                      • H honor3us

                        #include #include #include int main(int *op[]) {
                        char n;
                        int total;
                        int ct = 0, oper1 = 0, oper2 = 0, oper3 = 0, i = 0;
                        FILE *arq1;

                        arq1 = fopen("Maq1.log", "r"); //open file
                        while (!feof(arq1)) {
                        n = fgetc(arq1);
                        if (n == "\n") { //count lines
                        ct++;
                        }
                        }
                        op = (int*) calloc(ct, sizeof(int)); //alloc memory
                        if(op==0) {
                        printf("Não houve memoria alocada!\n");
                        return 0;
                        }
                        for(i=0;!feof(arq1);i++) {
                        fscanf(",,,%d,", &op[ct]); //I think this is the error
                        }
                        for(i=0;i<=ct;i++) { //do the count
                        if(op[ct] == 1){
                        oper1++;
                        }
                        if(op[ct] == 2){
                        oper2++;
                        }
                        if(op[ct] == 3){
                        oper3++;
                        }
                        }
                        total = oper1 + oper2 + oper3;
                        printf("Operacao 1: %d \n Operacao 2: %d \n Operacao 3: %d \n Total: %d \n", oper1, oper2, oper3, total); //and print!
                        fclose(arq1);
                        return 0;
                        }

                        Can you guys help me on this Code? I have no idea of what is happening, but when I run the code he prints

                        Operacao 1: 0
                        Operacao 2: 0
                        Operacao 3: 0
                        Total: 0

                        Sorry for the bad english!

                        H Offline
                        H Offline
                        honor3us
                        wrote on last edited by
                        #12

                        The program is working fine now, just needed to close the file and open again to read the data. Ty for the help everyone!

                        D 1 Reply Last reply
                        0
                        • H honor3us

                          The program is working fine now, just needed to close the file and open again to read the data. Ty for the help everyone!

                          D Offline
                          D Offline
                          David Crow
                          wrote on last edited by
                          #13

                          honor3us wrote:

                          ...just needed to close the file and open again...

                          Or call rewind().

                          "One man's wage rise is another man's price increase." - Harold Wilson

                          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                          "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups