File reading into array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nzjohnc
    New Member
    • Aug 2007
    • 3

    File reading into array

    Hi all,
    just started c ,need some help for storing values in arrays.I have opened the file for reading using fscanf,cos its got only three fields,but when i try to get the values to array,I'm getting the address values.If any one could give some advice on the procedure or concept of getting the values from a text file and storing them in arrays for further calculations would be a great help...
  • RRick
    Recognized Expert Contributor
    • Feb 2007
    • 463

    #2
    What kind of values are you trying to read in? Integers? Strings?

    A snippet of the code that doesn't work would help. (Please use [ code]...[/code] around the source code)

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      Please use a clearer title in the future. The title "c programming..." is very broad and does not provide other users with any idea of your question. A clearer title will help users identify your problem quicker and, thus, be able to give you more help.

      Thanks!

      Comment

      • nzjohnc
        New Member
        • Aug 2007
        • 3

        #4
        Originally posted by nzjohnc
        Hi all,
        just started c ,need some help for storing values in arrays.I have opened the file for reading using fscanf,cos its got only three fields,but when i try to get the values to array,I'm getting the address values.If any one could give some advice on the procedure or concept of getting the values from a text file and storing them in arrays for further calculations would be a great help...
        c programming help!!

        This is only the reading part that has been done.The file has a one character and two integer colums.The idea is to sum the values based on conditions(eg gender ,age group,preferred media).eg of data in the file F 29 1(200 of them),where F correspond to gender 29 to age and 1 to choice of media.I'm making an attempt to get the values into a 2 dimensional array,but need some help regarding the correct procedure to get the values into the array to start with.All tips welcome

        The items after //are part of the ongoing experiment but devoid of them ,the file opens for reading.
        [code=c]#include <stdio.h>
        #include<conio. h>

        //int survey[6][6];
        //int count;
        void main()
        {
        FILE *fp;
        fp=fopen("h:\\n fworks\\custsur vey.txt","r");
        //int i,j;
        //int sum_f,sum_m;
        char gender;
        int age,read;

        {
        //for(count=0;cou nt<36;count++)
        {
        while
        (fscanf(fp,"%c %d %d",&gender,&ag e,&read)!=EOF) ;


        //if(gender='F')
        {


        //sum_f+=i;
        //printf("%d",sum _f);

        }
        }
        printf("test ok....\n");
        if(fclose(fp)== EOF)
        puts("Cannot close file..");
        }
        puts("\n\n --------------");
        getch();
        }
        [/code]

        Comment

        • nzjohnc
          New Member
          • Aug 2007
          • 3

          #5
          Reading Text File and producing output in c language.
          Hi Guys here is what I have done so far.It still has a problem of not counting the vertical colums that is the sum total of different age groups.Could you give some idea on where to get the for loop going for going through all the elements of the array.I am stuck with that part now.Please help.
          Code:
          
          
          #include <stdio.h>
          #include <conio.h>
          
          
          #define Typeone(Gender) (Gender=='F') ? 0 : 1
          
          
          
          int Category2,CustomerType[2][3][6],LeadTypeCustomers[6];
          
          void Read();
          void TypeTwo(int Age);
          void Report();
          void PrintReportHeading(FILE *fp);
          void PrintSideHeadings(FILE *fp,int i);
          void PrintSurveyDetails(FILE *fp,int i,int j,int k);
          void PrintTotalSection(FILE *fp,int LeadGrandTotal);
          void PrintLeadTypeCustomerTotal(FILE *fp,int i);
          
          void main()
          {
          
              Read();
              Report();
          
          }
          
          void Read()
          {
          
              char Gender,NewLine;
          
          
          
              int Age,Lead,Category1,Category3=0;
          
          
          
              FILE *fp = fopen("h:\\nfworks\\custsurvey.dat","r");
          
              if(fp == NULL)
              {
          
          
          
                  printf("Error. Can't open the file");
                  getch();
          
              }
              else
              {
          
          
                  while(fscanf(fp,"%c %d %d",&Gender,&Age,&Lead)!=EOF)
                  {
          
          
                      Category1 =Typeone(Gender);
          
          
          
                      TypeTwo(Age);
          
                      Category3 = Lead-1;
          
          
          
                      CustomerType[Category1][Category2][Category3] = CustomerType[Category1][Category2][Category3] + 1;
          
          
                      fscanf(fp,"%c",&NewLine);   
          
                  }
          
                  if(fclose(fp)==EOF)
                  {
          
          
          
                  printf("\nError. Can't close the file");
                      getch();
          
                  }
          
              }
          
          }
          
          void TypeTwo(int Age)
          {
          
              if(Age<25)
              {
          
                  Category2 = 0;
          
              }
              else if(Age<=40)
              {
          
                  Category2 = 1;
          
              }
              else
              {
          
                  Category2 = 2;
          
              }
          
          }
          
          void Report()
          {
          
          
              int Count1,Count2,Count3,LeadTotal,LeadGrandTotal =0;
          
          
          	FILE *fp = fopen("h:\\nfworks\\project_test.txt","w");
          
              if(fp == NULL)
              {
          
          
                  printf("Error. Can't open the file");
                  getch();
          
              }
              else
              {
          
                  PrintReportHeading(fp);
          
                  for(Count1=0;Count1<6;Count1++)
                  {
          
          
                      PrintSideHeadings(fp,Count1);
          
                      LeadTotal=0;
          
                      for(Count2=0;Count2<2;Count2++)
                      {
          
          
          
          
                          for(Count3=0;Count3<3;Count3++)
                          {
          
                              
          
                              PrintSurveyDetails(fp,Count1,Count2,Count3);
          
                              LeadTotal = LeadTotal + CustomerType[Count2][Count3][Count1];
          
          
                              LeadTypeCustomers[Count1] = LeadTypeCustomers[Count1]
                                                                          +
                                                      CustomerType[Count2][Count3][Count1];
          
          
                          }
          
                      }
          
                      LeadGrandTotal = LeadTotal+LeadGrandTotal;
          
                      fprintf(fp,"\t %d",LeadTotal);
          
                  }
          
          
          
                  PrintTotalSection(fp,LeadGrandTotal);
          
                  if(fclose(fp))
                  {
          
          
                      printf("Error. Can't close the file.");
                      getch();
          
                  }
          
              }
          
          }
          
          void PrintReportHeading(FILE *fp)
          {
          
              fprintf(fp,"\t\t\t    CUSTOMER SURVEY REPORT\n");
              fprintf(fp,"\t\t\t    **********************\n");
              fprintf(fp,"\t\t      FEMALES\t\t\tMALES\n");
              fprintf(fp,"\t      ***********************  ***********************\n");
              fprintf(fp,"\t      younger  range   older   younger  range   older\n");
              fprintf(fp,"\t      than 25 25 - 40 than 40  than 25 25 - 40 than 40\n");
              fprintf(fp,"    LEAD TYPE ***********************  *********************** TOTAL\n");
              fprintf(fp,"    *********\t\t\t\t\t\t       *****\n\n");
          
          }
          
          
          void PrintSideHeadings(FILE *fp,int i)
          {
          
              switch (i)
              {
          
                  case (0):fprintf(fp,"       REPEAT");
                           break;
                  case (1):fprintf(fp,"\n\nTELEVISION AD");
                           break;
                  case (2):fprintf(fp,"\n\n NEWSPAPER AD");
                           break;
                  case (3):fprintf(fp,"\n\n     RADIO AD");
                           break;
                  case (4):fprintf(fp,"\n\nWORD OF MOUTH");
                           break;
                  case (5):fprintf(fp,"\n\n\tOTHER");
                           break;
              }
          
          }
          
          void PrintSurveyDetails(FILE *fp,int i,int j,int k)
          {
          
          
              if(j==0)
              {
          
                  fprintf(fp,"\t %3d",CustomerType[j][k][i]);
          
              }
              else 
              {
          
                  fprintf(fp,"\t%3d",CustomerType[j][k][i]);
          
              }
          
            }
          
          
          void PrintTotalSection(FILE *fp,int LeadGrandTotal)
          {
          
          
              int i,AgeRangeGrantTotal = 0;
          fprintf(fp,"\n\t*****  ****    ****    ****     ****    ****    ****   ****\n");
             
              fprintf(fp,"\tTOTAL");
          
              for(i=0;i<6;i++)
              {
          
          
                  /*PrintLeadTypeCustomerTotal(fp,i);*/
          
                  AgeRangeGrantTotal =AgeRangeGrantTotal + LeadTypeCustomers[i];
          
              }
          
          
          
              //if(LeadGrandTotal==AgeRangeGrantTotal)
              {
          
                  fprintf(fp,"\t%d",AgeRangeGrantTotal);
          
              }
               
          
          	 fprintf(fp,"\n\n\t*****  ****    ****    ****     ****    ****    ****   ****\n");
           
          }
          
          void PrintLeadTypeCustomerTotal(FILE *fp,int i)
          {
          
          
          
              if(i == 0)
              {
          
                  fprintf(fp,"  %4d",LeadTypeCustomers[i]);
          
              }
              else if((i == 1)||(i == 2))
              {
          
                  fprintf(fp,"    %4d",LeadTypeCustomers[i]);
          
              }
              else
              {
          
                  fprintf(fp,"\t%4d",LeadTypeCustomers[i]);
          
              }
          
          }

          Comment

          Working...