Here is a data file like this,please help me update my code to omit all comments stored in the file.sample file looks like this:

C:\\s1.txt
#author:Single Ray
#data:02-2-10
141.641, 567.614, 24.25
140.939, 567.736, 24.25 #start
140.053, 567.947, 24.25
148.985, 568.352, 24.25 #end
....

Im sorry,forgot attach my codes,here is my code which could only import data lines have no comments!!Please help me out!!

#include "stdio.h"

void main()
{
  int i=0; 
  char pound; //still working on omit those comments
 
  double a[143][2];
  FILE *fp;
  fp=fopen("s1.txt","r");
  if(fp==NULL) {
   
            printf("Error: can't open file.\n");
 
            return 1;
                }
    else {
            printf("File opened successfully. Contents:\n\n");
   
           while(!feof(fp))
            {
            
            for(i=0;i<144;i++){
                    
            fscanf(fp,"%lf%*s%lf%*s%lf\n",&a[i][0],&a[i][1],&a[i][2]);
           
            printf("(%lf, %lf, %lf)\n",a[i][0],a[i][1],a[i][2]);
              
                               } 
            }
            fclose(fp);
            return 0;
         }
}

Recommended Answers

All 7 Replies

It's a shame you didn't pay any attention to any of the many places which describe code tags.

But I'm sure someone will be along to fix that at some point, and later still, someone else might get to look at it.

Thank you!!

#include "stdio.h"

void main()
{
  int i=0; 
  char pound; //still working on omit those comments
 
  double a[143][2];
  FILE *fp;
  fp=fopen("s1.txt","r");
  if(fp==NULL) {
   
            printf("Error: can't open file.\n");
 
            return 1;
                }
    else {
            printf("File opened successfully. Contents:\n\n");
   
           while(!feof(fp))
            {
            
            for(i=0;i<144;i++){
                    
            fscanf(fp,"%lf%*s%lf%*s%lf\n",&a[i][0],&a[i][1],&a[i][2]);
           
            printf("(%lf, %lf, %lf)\n",a[i][0],a[i][1],a[i][2]);
              
                               } 
            }
            fclose(fp);
            return 0;
         }
}

It's a shame you didn't pay any attention to any of the many places which describe code tags.

But I'm sure someone will be along to fix that at some point, and later still, someone else might get to look at it.

Member Avatar for iamthwee

Ignoring all your other problems, void main feof, etc.

I would just take the line, look for the first instance of # find it's position then to a print of all chars from 0 - position.

Eg.

#include <stdio.h>
#include <string.h>

int main ( void )
{
  char test[] = " hei fjdkls jfklds #fdjskfjdsl";

  int i;

  int size = strlen ( test );

  int pos;
  for ( i = 0; i < size; i++ )
  {
    if ( test[i] == '#' )
    {
      pos = i;
      break;
    }
  }

  /*print out line without comments*/
  for ( i = 0; i < pos; i++ )
  {
    printf ( "%c", test[i] );
  }

  /*printf("%d",pos); */
  getchar();

  return 0;
}

output

hei fjdkls jfklds

> double a[143][2];
You need [3] in the minor dimension.
From looking at your code, you probably need to add 1 to the other as well.

> while(!feof(fp))
Try something like this

char buff[BUFSIZ];
while ( i < 144 && fgets( buff, sizeof buff, fp ) != NULL ) {
  if ( (sscanf(buff,"%lf%*s%lf%*s%lf",&a[i][0],&a[i][1],&a[i][2])) == 3 ) {
    i++;
  }
}

In all but the most trivial of programs, separating "input" from "conversion" is a much easier way to go. You'll get a much more predictable program as a result.


Oh, and as already mentioned, void main is wrong as well. It returns an int.

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.