i have a program in c language that reads csv file..

now i want to read data column wise..
i want the whole program..which will take file name as command line argument and will fetch
second column from the file..

lets assume file is like below..

aggromat,"1","abc","8903"
aggromat,"2","abc","ddds"

reply as soon as possible.

Read the entire file and extract the column(s) you need. I would use fgets() to read a line, then strtok() to split it into individual columns.

i have successfully read a csv file.. from which m gettin time in (ist)..
now another csv file is containing following format..

7,30,25,am
8,45,25,pm ... and so on... which indicates time.. hours,min,sec respectively..

now i need to divide column of min by 60 and need to concatenate result wid hours..
in short i want to generate time in decimal form..

i m not able to get min values in string..
hv written some code..

#include <stdio.h>
#include <stdlib.h>

int main()
{
  FILE *in, *out;
  char ch[2000],str[1000];


  char *rdf,*wdf;
  int comma_count = 0;
  int wait_for_newline = 0;
  int i,k;
  char strm[100],strh[100];
  char strf[200];
  clrscr();

   rdf="A:\\r4.csv";
   wdf="A:\\r5.csv";

  in = fopen(rdf,"r");
  out = fopen(wdf,"w");

  if(in == NULL)
  {
    printf("Cannot open input file.\n");
    exit(1);
  }

  if(out == NULL)
  {
    printf("Cannot open output file.\n");
    exit(1);
  }

  while(!feof(in))
  {
    *ch = getc(in);
    if(ferror(in))
    {
      printf("Read Error");
      clearerr(in);
      break;
    }
    else
    {
      if(!feof(in))
      {
    if(ch=='\n')
    {
        wait_for_newline = 0;
        comma_count = 0;
    }

    if(ch==',')
    {

        comma_count++;


    }
    if(comma_count == 0 && wait_for_newline==0)
    {
        if(ch==',')
        {
           putc('\n', out);
        }
        else
        {
          strm[i]= getc(in);
          i++;
        }
        printf("%s\n",strm);



    } /*
    if(comma_count == 1 && wait_for_newline==0)
    {
        printf("\nhelo2");
    }   */

      }
      if(ferror(out))
      {
    printf("Write Error");
    clearerr(out);
    break;
      }
    }
  }
  printf("\n File has been successfully created.");

  fclose(in);
  fclose(out);
  getch();
  getch();
  return 0;
}

plz help me..

as soon as possible

>>now i need to divide column of min by 60

Why? Does the minute column ever exceed 60? If not then there is no point doing that division.

actually i need to convert time in decimal form.
for example 7 :30 -> 7.50 ...
so i need to divide min by 60 first..

if dis can be done in easier way let me know.

i have excel sheet with lots of data... time date etc..
what i am doin is fetching date in intermediate file.. den m working on it..

if it is possible to convert time in decimal while fetching den let me know..

note that m working on csv file.

You mean like this?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#pragma warning(disable: 4996)

int main()
{
    int hour,minute,second;
    char ampm[4];
    FILE* fp = fopen("TextFile1.txt","r");
    if( fp == NULL)
    {
        printf("Failed to open file\n");
        return 1;
    }
    while( fscanf(fp,"%d,%d,%d,%s\n", &hour,&minute,&second,ampm) > 0)
    {
        minute = (int)(((float)minute/60.0) * 100);
        printf("%d.%d\n", hour,minute);

    }
    getchar();

}

as i said.. m working on csv...
it ll be in form of

sdfs,dfsdf,dfsdf,hgffd,9:30,28-01-1990,kjdfk,dkjfhk

sdjf,fdfgdf,fdgd,ffdgf,10:20,28-9-1999,dsfkf,dfsdf

and so on..
so ur code won' work in such kind of file..
got to fetch 5th column first.
den need to separate time in hours n min..
and den convert dem into decimal..

not text file..
its .csv file.

Will you please make up your mind about the file format. The code I posted works with the format you original posted. You can easily enhance the program to suit whatever format is the file by changing the fscanf() line to accommodate more parameters. Its not possible to read just the time that's in the middle of the line -- read evrything on the line.

And, please spell English words -- its "then" not "den". You sound like a 2-year-old when you write like that.

sorry for the bad English.
alrite. i am having bad situation over here.
i will try your code and will let u know the final file format, final requirement and the code i have written by evening.

i can't read everything on the same line. i have to do work with time only.
and that is the main problem.
if i read the whole line then i have to read almost 30 parameters. which is quite complicated.

You don't have to read it all at one time, but you do have to read all of it. Use a loop to read unwanted fields and just toss them out

char buf[255];
// read first 10 fields
int i;
for(i = 0; i < 10; i++)
   fscanf(fp, "%s,",buf);  // something like this
// now read the times.  After that, 
// call fgets() to read the remainder of the line
//

Another way to do it is to read the entire line with fgets() then use strtok() to split it into its individual fields, ignoring unwanted fields at the beginning of the line.

hey. thank you.
your first code helped me in another program.

i have some other doubt now. i want to use exp function in that program.
i have written code like following:

.
.
.
svp1 = (atof(str1) * 6.78 + 430);
svp2 = (stof(str2) * 6.78 + 430);

fprintf(out,"%f,%f\n",exp(svp1),exp(svp2));

it is not working.
gives error that undefined reference "exp".
note that i have included <math.h>

make sure to include math.h

i have included.
i have specified in my question.
still getting such error.
any solution?

post the entire code

> gives error that undefined reference "exp"

Including math.h is not enough. You need to link with the actual math library. How exactly do that depends on your compiler. Check the documentation.

i can't post code from office. it only allows me to surf.
i will do it from home.

and i got the problem. i am using gcc compiler.
and need to write -lm somewhere .
can u tell me where?

i am doing this:
to compile : gcc -o new prog.c
to run : ./new

now how to link with actual library?

just add -lm on the same line as gcc, such as gcc -o new prog.c -lm

okay. i will try this.
i've written a code like following:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
  FILE *in, *out,*in2;
  char ch,str[10],str1[10],str2[10],str3[10];
  char *rdf,*wdf,*rdf2;
  float svp1,svp2,svp3;
  float avp1,avp2,avp3;
  double exsvp1,exsvp2,exsvp3;
  char rh1[10],rh2[10],rh3[10];

  clrscr();

   rdf="A:\\cvp.csv";

   wdf="A:\\a.csv";

  in = fopen(rdf,"rb");
 // in2 = fopen(rdf2,"rb");
  out = fopen(wdf,"w");

  if(in == NULL)
  {
    printf("Cannot open input file.\n");
    exit(1);
  }

  if(out == NULL)
  {
    printf("Cannot open output file.\n");
    exit(1);
  }

	while(fscanf(in,"%s %s %s %s %s %s\n,",&str1,&str2,&str3,&rh1,&rh2,&rh3)>0 )
	{
		svp1 = ((atof(str1) * 21.4) + 494.41 ) / (atof(str1) + 273.15);
		svp2 = ((atof(str2) * 21.4) + 494.41 ) / (atof(str2) + 273.15);
		svp3 = ((atof(str3) * 21.4) + 494.41 ) / (atof(str3) + 273.15);
		printf("%f,%f\n",svp1,svp2);
		exsvp1 = exp(svp1);
		exsvp2 = exp(svp2);
		exsvp3 = exp(svp3);		
		avp1= (atof(rh1)/100) * exsvp1;
		avp2= (atof(rh2)/100) * exsvp2;
		avp3= (atof(rh3)/100) * exsvp3;
		fprintf(out,"%f %f %f %f %f %f \n",exsvp1,exsvp2,exsvp3,avp1,avp2,avp3);
		printf("%f %f %f",avp1,avp2,avp3);
		
	}
	printf("\nFile has successfully written");
	fclose(in);
	fclose(out);
	getch();
	getch();
	return 0;
}

this works properly in turbo c..
when i deal with linux it says segmentation fault.
i've deleted clrcsr() n getch()..

problem is the first line of my csv file which is :
AGRO_ST1, AGRO_ST2, AGTO_ST3,..

n rest all line is int/float ...
so if i delete first line from csv file then it works properly.
otherwise gives error.

i've some idea of solution using sscanf .. but how i don't know..!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.