hello all,

following is the code that fetches required columns from r.csv and makes a new file.

now i want to ignore first line of r.csv while fetching defined column .
what should be done?

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

int main()
{
  FILE *in, *out;
  char ch;
  char *rdf,*wdf;
  int comma_count = 0;
  int wait_for_newline = 0;

 clrscr();

   rdf="A:\\r.csv";
   wdf="A:\\abl.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 == 8 && wait_for_newline==0))
	{
		if(ch==',')
		putc('\n', out);
		else
		putc(ch, out);
	}
	if(comma_count == 9 && wait_for_newline == 0)
	{
		if(ch==',')
		putc(' ', out);
		else
		putc(ch, out);
	}
	if(comma_count == 10 && wait_for_newline == 0)
	{
		if(ch==',')
		putc(' ', out);
		else
		putc(ch, out);
	}
	if(comma_count == 11 && wait_for_newline == 0)
	{
		if(ch==',')
		putc(' ', out);
		else
		putc(ch, out);
	}
	if(comma_count == 12 && wait_for_newline == 0)
	{
		if(ch==',')
		putc(' ', out);
		else
		putc(ch, out);
	}
	if(comma_count == 13 && wait_for_newline == 0)
	{
		if(ch==',')
		putc(' ', out);
		else
		putc(ch, out);
	}
	if(comma_count == 26 && wait_for_newline == 0)
	{
		if(ch==',')
		putc(' ', out);
		else
		putc(ch, out);
	}
	if(comma_count == 27 && wait_for_newline == 0)
	{
		if(ch==',')
		putc(' ', out);
		else
		putc(ch, out);
	}
	if(comma_count == 28 && wait_for_newline == 0)
	{
		if(ch==',')
		putc(' ', out);
		else
		putc(ch, out);
	}
	if(comma_count == 29 && wait_for_newline == 0)
	{
		if(ch==',')
		putc(' ', out);
		else
		putc(ch, out);
	}
	if(comma_count == 30 && wait_for_newline == 0)
	{
		if(ch==',')
		putc(' ', out);
		else
		putc(ch, out);
	}
      }
      if(ferror(out))
      {
	printf("Write Error");
	clearerr(out);
	break;
      }
    }
  }
  printf("\n File has been successfully created.");

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

Recommended Answers

All 9 Replies

Just call fgets() to read the first line before starting the loop on line 32.

I think this would be a lot easier if you used fgets() to read every line, then parse it by using strtok()

char buf[255];
fgets(buf, sizeof(buf), in); // just ignore the first line

while( fgets(buf,sizeof(buf),in) // read the rest of the file
{
   char* ptr = strtok(buf,",");
   int counter = 1;
   while( ptr != NULL && counter < 9)
   {
        ptr = strtok(NULL,",");
        counter++;
   }
   // got the 9th column, so do something with it

   ????
}

First, don't use feof() to test for the end of file - it doesn't work as you think, and you'll frequently get doubled last data.

Second, to ignore a row of test, just pull it into a variable that will be immediately overwritten, outside of the overall loop that you use to read the file with.

i tried using strtok but i had lots of trouble with that earlier.
and i want a file separated by space, not by comma ..
so i did this way..


and what should i write instead of feof() ??

Well, just replace the comma with a space. What kind of trouble did you have -- the code snippet I posted works very well. If you need the original string to do something else then you have to copy it into another buffer for strtok() to use because strtok() replaces the tokens with 0's. If your intention is to change the value of a certain column and write the new string into another file then maybe strtok() isn't the best solution afterall. You can do something similar with strchr()) to find the spaces in the string.

well while using strtok(), i used to face troubles like " unable to convert pointer to integer" or something like dat.

for ex,

int *i,*j;

i = strtok(Line,":");
j= strtok(null, ":");

then also it gave me errors.

well while using strtok(), i used to face troubles like " unable to convert pointer to integer" or something like dat.

for ex,

int *i,*j;

i = strtok(Line,":");
j= strtok(null, ":");

then also it gave me errors.

Ancient Dragon already laid out how to read a file, if you want to read the entire file, row by row:

while( fgets(buf,sizeof(buf),in) // read the rest of the file
{
   //your other code in here, will strcpy() buff array
   //into another string or string array. First, you could
   //remove all the comma's in the string, very easily.
   //One of the beautiful things about using fgets().
}

You'll be doing yourself a favor to make a note of the above, and get really familiar with it. Very useful indeed.

I would use sscanf() to get the column you want's data, out of the row. I don't really prefer strtok(), for you, since it modifies the string, as it's working.

Look at that! I already gave you this same advice. :(

No offense, but you need to either work on reading or studying the replies you're being given, in the forum. You're wasting people's time, as it is. Here's my previous reply to you, in your other thread on this assignment:

make a char buff[100] array, to hold one line of text
make a float data[SIZE][COLS]; //fit your sizes for rows and columns.Leave room for 
end of string char and newline char, in your column number  
still have index[SIZE];

while((fgets(buff, sizeof(buff), filePointerName)) != NULL) {
  ok = sscanf(buff, "%*s %*s %f", floats[i]; //skip the 2 leading strings, store the float number into an array of floats
  if(ok > 0) {
    strcpy(data[i], buff);//copy buff string (including float), into data[i]
    ++i;
  }
}

well while using strtok(), i used to face troubles like " unable to convert pointer to integer" or something like dat.

for ex,

int *i,*j;

i = strtok(Line,":");
j= strtok(null, ":");

then also it gave me errors.

strtok() returns a pointer to a char, not a pointer to an int -- it only works with strings, never integers.

okay.. i got my problem...
thank you very much for your help and suggestions..:)

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.