3 Replies - 1423 Views - Last Post: 02 August 2009 - 08:24 PM Rate Topic: -----

#1 jmgriffin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-August 09

do while syntax checking for no space

Post icon  Posted 02 August 2009 - 12:54 PM

I am trying to save a textual input file of double numbers and save to an array using C code in MS Visual C++ 2008 Express Edition ....I have looked at lots of code examples on the net and Programming C with Brian W. Kernighan and Denis M. Ritchie however I can't understand the syntax error in the do while loop of testme the != should be checking for no space and new line however there's a syntax error with != ...feel this could be the pinter to an array that is the problem - can anyone help please?

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



int main ( void )
{
static const char filename[] = "test.txt";
static const char filename1[] = "test1.txt";
//static const char filename2[] = "test2.txt";
//static const char filename1[] = "ReadMe.txt";
FILE *file = fopen ( filename, "r" );
FILE *file1 = fopen( filename1, "w");
//FILE *file2 = fopen( filename2, "r");
if ( file != NULL )
{
char line [ 128 ]; /* or other suitable maximum line size */
//int numofline,i,
int element[128], a=0;
//char array1[128]
while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
{
fputs ( line, stdout ); /* write the line */
//array1 = line; /* write to file after manipulation */
a=testme(element,line); /*Put data into an array*/
fputs ( line, file1); /* look to merge with basic AI classification code */
}

fclose ( file );
fclose ( file1 );
}
else
{
perror ( filename ); /* why didn't the file open? */
}
//if ( file2 != NULL )
//{
// double u;
// if (GetDValue(file2, u))
// return -1;
//}
/* FILE *file = fopen ( filename1, "r" );

if ( file != NULL )
{
char line [ 128 ]; // or other suitable maximum line size
*/

/*
while ( fgets ( line, sizeof line, file ) != NULL ) read a line */
// {
// fputs ( line, stdout ); /* write the line */
/* write to file after manipulation */
/* look to merge with basic AI classification code */
/* }
fclose ( file );
}
else
{
perror ( filename ); /* why didn't the file open?
} */

return 0;
}


int testme(int *ele,char *new)
{ int i=-1,j=0,k=0,linelength=strlen(new);char *piece;
do
{
j=0;printf("\0");
do /*Loop for data separation*/
{ i++;
piece[j++]=new[i]; /*buffer=data*/
}
while((new[i]!= ' ')&&(new[i]!= '\0')); /*check for
//space between data and end of line*/
ele[k]=atoi(piece); /*Put data into an array*/
*piece='\0'; /*reset buffer*/
k=k+1;
}
while(i<linelength);
k=ele[0];ele=&ele[1]; /*Remove first item (# of data)*/
return k; /*Return # of data*/
}

// read in colunms of data as float and store accordingly

Is This A Good Question/Topic? 0
  • +

Replies To: do while syntax checking for no space

#2 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: do while syntax checking for no space

Posted 02 August 2009 - 01:04 PM

An error that i can see right now is:

while(fgets(line,sizeof line ,file)


should be written like this:

while(fgets(line,sizeof(line),file)

Was This Post Helpful? 0
  • +
  • -

#3 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: do while syntax checking for no space

Posted 02 August 2009 - 01:46 PM

@poncho4all

That is not an error. It is completely legal.

I would have to review my C File IO before I could help you.

What about this line?
else
{
perror ( filename ); /* why didn't the file open?
} */


You are commenting the brace out.
Was This Post Helpful? 0
  • +
  • -

#4 Guest_c.user*


Reputation:

Re: do while syntax checking for no space

Posted 02 August 2009 - 08:24 PM

	char *piece;

...

	piece[j++]=


Was This Post Helpful? 0

Page 1 of 1