Thread: ERROR OPENING A FILE IN c

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    2

    ERROR OPENING A FILE IN c

    hello everyone, im having a headache with this error im getting, im trying to read a number from a file but inside a Switch(case) statement but im getting alot of erros only inside the case and i cant figure out why if my open file code is correct (as far as i know) i need to read a number from the file and make a + with my (ac) variable

    heres my code



    • #include<stdio.h>
    • #include<stdlib.h>
    • int main ()
    • {
    • int pc;
    • int ir;
    • int cod;
    • int ac=0;
    • int i=0;
    • int dir;
    • int c=0;
    • int num_datos;
    • int dir_datos=0;
    • int MAX=0;
    • int x=0;
    • int valor[1000];
    • printf("Formato de los datos 0000\n\n");
    • /* printf("DATOS\n\n");
    • printf("100\t0005\n")
    • printf("101\t0002\n\n"); */
    • printf("\n");
    • printf("Cuantas instrucciones vas a capturar?\n");
    • scanf("%d",&dir);//5
    • printf("\n");
    • printf("Escriba la direccion de la primer instruccion\n");
    • scanf("%d",&pc);//100
    • printf("\n");
    • // printf("Escriba el valor de la direccion de memoria\n");
    • // scanf("%d",&ir);//1200
    • printf("Escriba Cuantos datos van a haber\n");
    • scanf("%d",&num_datos);//3
    • printf("\n");
    • printf("Escriba la direccion de los datos\n");
    • scanf("%d",&dir_datos);//200
    • MAX=num_datos+dir_datos;
    • printf("\n");
    • for(i=dir_datos; i<MAX; i++)
    • {
    • printf("Escriba el valor del dato [%d]\n",dir_datos);
    • scanf("%d",&c);
    • valor[i]=c;
    • dir_datos++;
    • printf("\n");
    • }
    • dir_datos=dir_datos-num_datos;
    • for(i=dir_datos; i<MAX; i++)
    • {
    • printf("%d \t %d\n",i,valor[i]);
    • }
    • printf("\n");
    • // cod=ir/1000;
    • // dir=ir%1000;
    • //for(i=0; i<dir; i++)
    • while(x<dir)
    • {
    • printf("Escriba el valor de la direccion de memoria\n");
    • scanf("%d",&ir);//1200
    • cod=ir/1000;//este ya esta listo
    • // Mem=ir%1000;//el % significa el resto de la division
    • dir_datos=ir%1000;
    • switch(cod)//esta correctamente evaluado
    • {
    • case0:
    • printf("tomaste el caso 0\n");
    • printf("%d \t %d \t %d\n",pc,ir,ac);
    • pc++;
    • x++;
    • break;
    • case1:
    • printf("tomaste el caso 1\n");
    • for(i=dir_datos; i<MAX; i++)
    • {
    • if(i==dir_datos)
    • {
    • printf("%d \t %d \t %d\n",pc,ir,ac);
    • ac=valor[dir_datos];
    • }
    • }
    • //Mem=ir%1000;
    • // printf("i=%d\n",i);
    • // ac=Mem[dir];
    • pc++;
    • x++;
    • break;
    • case2:
    • printf("tomaste el caso 2\n");
    • for(i=dir_datos; i<MAX; i++)
    • {
    • if(i==dir_datos)
    • {
    • ac=valor[dir_datos];
    • printf("%d \t %d \t %d\n",pc,ir,ac);
    • }
    • }
    • pc++;
    • x++;
    • break;
    • case3:
    • printf("tomaste el caso 3\n");
    • for(i=dir_datos; i<MAX; i++)
    • {
    • if(i==dir_datos)
    • {
    • ac=ac+valor[dir_datos];
    • printf("%d \t %d \t %d\n",pc,ir,ac);
    • }
    • }
    • pc++;
    • x++;
    • break;
    • case4:
    • printf("tomaste el caso 4\n");
    • for(i=dir_datos; i<MAX; i++)
    • {
    • if(i==dir_datos)
    • {
    • ac=ac-valor[dir_datos];
    • printf("%d \t %d \t %d\n",pc,ir,ac);
    • }
    • }
    • pc++;
    • x++;
    • break;
    • case5:
    • printf("tomaste el caso 5\n");
    • for(i=dir_datos; i<MAX; i++)
    • {
    • if(i==dir_datos)
    • {
    • ac=ac*valor[dir_datos];
    • printf("%d \t %d \t %d\n",pc,ir,ac);
    • }
    • }
    • pc++;
    • x++;
    • break;
    • case6:
    • printf("tomaste el caso 6\n");
    • for(i=dir_datos; i<MAX; i++)
    • {
    • if(i==dir_datos)
    • {
    • ac=ac/valor[dir_datos];
    • printf("%d \t %d \t %d\n",pc,ir,ac);
    • }
    • }
    • pc++;
    • x++;
    • break;
    • ///////////////////////////////////////////////////////////////////////////////////////////
    • case7:
    • // int producto ( int argc, char **argv )
    • //{int c;
    • FILE*archivo;
    • //char buffer[100];
    • int c;
    • archivo = fopen ("archivo.txt","r");
    • while((c = fgetc(archivo))!= EOF){
    • ac=ac+c;
    • }
    • // fscanf(archivo, "%s" ,buffer);
    • //buffer=atoi(buffer);
    • // ac=ac+buffer;
    • printf("Convirtiendo la cadena %s en un numero: %d\n",buffer);
    • printf("%d \t %d \t %d\n",pc,ir,ac);
    • fclose ( archivo );
    • return0;
    • //}
    • pc++;
    • x++;
    • break;
    • /////////////////////////////////////////////////////////////////////////////////////////////////
    • case8:
    • printf("tomaste el caso 8\n");
    • printf("%d \t %d \t \n",pc,ir);
    • //8 Escribe AC en archivos.txt
    • pc++;
    • x++;
    • break;
    • }
    • }
    • return0;


    Last edited by xixzeroxix; 11-01-2012 at 02:55 PM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Egad!!! What the heck is that!?!? Please post your code in a more readable manner, using code tags (more info here: Announcements - General Programming Boards), instead of a bullet-point list. A couple more tips to make it readable:
    1. Make sure to copy-paste as plain text, our forum will handle syntax highlighting.
    2. Properly indent your code, so it looks nice, and don't mix tabs and spaces. Using only spaces for indentation will help ensure it looks nice on the forum.
    3. Don't use other fonts for regular text. The forum's default font is fine.

    Then, describe in more detail what kind of errors you are getting. Are they compiler errors? If so, copy-paste them exactly, with the line numbers. Does it crash? Does it (appear to) do nothing? Does it produce the wrong output? If so, what input did you give it, what output do you get, and what output should you get?

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Impossible to tell if this is an artifact of how you managed to post the code, but this:

    Code:
    case0:
    is not how you declare a case label. You need a space between case and the label:

    Code:
    case 0:

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I've got a script which usually cleans up the font/colour barfs that appear, but this was too much.

    Here, this is what your code should look like when posted. Make sure you "paste as text" next time.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
      int pc;
      int ir;
      int cod;
      int ac = 0;
      int i = 0;
      int dir;
      int c = 0;
      int num_datos;
      int dir_datos = 0;
      int MAX = 0;
      int x = 0;
    
    
      int valor[1000];
    
    
    
      printf("Formato de los datos 0000\n\n");
      /*      printf("DATOS\n\n");
         printf("100\t0005\n")
         printf("101\t0002\n\n");   */
      printf("\n");
      printf("Cuantas instrucciones vas a capturar?\n");
      scanf("%d", &dir);            //5
      printf("\n");
      printf("Escriba la direccion de la primer instruccion\n");
      scanf("%d", &pc);             //100
      printf("\n");
      //  printf("Escriba el valor de la direccion de memoria\n");
      //  scanf("%d",&ir);//1200
      printf("Escriba Cuantos datos van a haber\n");
      scanf("%d", &num_datos);      //3
      printf("\n");
      printf("Escriba la direccion de los datos\n");
      scanf("%d", &dir_datos);      //200
      MAX = num_datos + dir_datos;
      printf("\n");
      for (i = dir_datos; i < MAX; i++) {
    
        printf("Escriba el valor del dato [%d]\n", dir_datos);
        scanf("%d", &c);
        valor[i] = c;
        dir_datos++;
        printf("\n");
      }
      dir_datos = dir_datos - num_datos;
      for (i = dir_datos; i < MAX; i++) {
        printf("%d \t %d\n", i, valor[i]);
      }
    
    
    
      printf("\n");
    
    
      //  cod=ir/1000;
      //   dir=ir%1000;
    
    
      //for(i=0; i<dir; i++)
    
      while (x < dir) {
    
        printf("Escriba el valor de la direccion de memoria\n");
        scanf("%d", &ir);           //1200
    
    
        cod = ir / 1000;            //este ya esta listo
        //   Mem=ir%1000;//el % significa el resto de la division
        dir_datos = ir % 1000;
    
    
        switch (cod)                //esta correctamente evaluado
        {
        case 0:
    
    
          printf("tomaste el caso 0\n");
          printf("%d \t %d \t %d\n", pc, ir, ac);
          pc++;
          x++;
    
          break;
    
        case 1:
    
          printf("tomaste el caso 1\n");
          for (i = dir_datos; i < MAX; i++) {
            if (i == dir_datos) {
              printf("%d \t %d \t %d\n", pc, ir, ac);
              ac = valor[dir_datos];
            }
          }
    
          //Mem=ir%1000;
          // printf("i=%d\n",i);
          // ac=Mem[dir];
          pc++;
          x++;
    
          break;
    
        case 2:
    
          printf("tomaste el caso 2\n");
          for (i = dir_datos; i < MAX; i++) {
            if (i == dir_datos) {
              ac = valor[dir_datos];
              printf("%d \t %d \t %d\n", pc, ir, ac);
            }
          }
          pc++;
          x++;
    
          break;
    
        case 3:
    
          printf("tomaste el caso 3\n");
          for (i = dir_datos; i < MAX; i++) {
            if (i == dir_datos) {
              ac = ac + valor[dir_datos];
              printf("%d \t %d \t %d\n", pc, ir, ac);
            }
          }
          pc++;
          x++;
    
          break;
    
        case 4:
    
          printf("tomaste el caso 4\n");
          for (i = dir_datos; i < MAX; i++) {
            if (i == dir_datos) {
              ac = ac - valor[dir_datos];
              printf("%d \t %d \t %d\n", pc, ir, ac);
            }
          }
          pc++;
          x++;
    
          break;
    
        case 5:
    
          printf("tomaste el caso 5\n");
          for (i = dir_datos; i < MAX; i++) {
            if (i == dir_datos) {
              ac = ac * valor[dir_datos];
              printf("%d \t %d \t %d\n", pc, ir, ac);
            }
          }
          pc++;
          x++;
    
          break;
    
        case 6:
    
          printf("tomaste el caso 6\n");
          for (i = dir_datos; i < MAX; i++) {
            if (i == dir_datos) {
              ac = ac / valor[dir_datos];
              printf("%d \t %d \t %d\n", pc, ir, ac);
            }
          }
          pc++;
          x++;
    
          break;
    ///////////////////////////////////////////////////////////////////////////////////////////
        case 7:
    
    
    // int producto ( int argc, char **argv )
    //{int c;
          FILE * archivo;
    
          //char buffer[100];
          int c;
          archivo = fopen("archivo.txt", "r");
          while ((c = fgetc(archivo)) != EOF) {
            ac = ac + c;
          }
          // fscanf(archivo, "%s" ,buffer);
          //buffer=atoi(buffer);
          //   ac=ac+buffer;
          printf("Convirtiendo la cadena  %s  en un numero: %d\n", buffer);
    
          printf("%d \t %d \t %d\n", pc, ir, ac);
          fclose(archivo);
    
          return 0;
    //}
    
    
          pc++;
          x++;
          break;
    /////////////////////////////////////////////////////////////////////////////////////////////////
        case 8:
    
          printf("tomaste el caso 8\n");
          printf("%d \t %d \t \n", pc, ir);
          //8 Escribe AC en archivos.txt
          pc++;
          x++;
    
          break;
    
    
        }
      }
      return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 11-30-2011, 12:49 AM
  2. "Error opening file, Segmentation Error"
    By Mojo in forum C Programming
    Replies: 12
    Last Post: 12-09-2010, 06:01 PM
  3. error opening the file...
    By roaan in forum C Programming
    Replies: 20
    Last Post: 07-27-2009, 03:42 PM
  4. No Matching Function Error When Opening File
    By zephyrcat in forum C++ Programming
    Replies: 19
    Last Post: 07-15-2008, 01:36 PM
  5. Error opening file
    By WackoWolf in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2004, 03:56 PM

Tags for this Thread