6 Replies - 748 Views - Last Post: 14 October 2012 - 10:45 PM Rate Topic: -----

#1 smichels  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-October 12

Open file to read, compare string array, print back to the file.

Posted 14 October 2012 - 12:33 PM

Good day, new kid on the block here.

I need to command a user to open file for reading and then take the words
ending in "ed" and print them back to the file.

My problem is I understand opening a file, and comparing the strings but
putting them together so that I am writing the new items to the file
just boggles my poor soul.

So a few issues here: can't actually open the text file, and also not sure how to go about reading the array into it.
I appreciate all of your help, and please do not give me short replies so as to assume I know exactly what you mean because I am a sincere virgin to c++. thanks





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

int main( void )
 {
 
int i; /* loop counter */
 int length; /* length of current string */
char array[ 5 ][ 20 ] = {0}; /* 5 strings from user */
 char filename [100];
 FILE* fileptr;

 printf ("Please enter the file to open:");
 scanf_s ("%s", filename);

 fileptr = fopen (filename, "r");

 if (fileptr == NULL)
 {
	 printf ("Unable to open '%s' for input. \n", filename);
 }

 else 
 {
	 while (!feof(fileptr))
 /* read in 5 strings from user */
 for ( i = 0; i <= 4; i++ ) {
 printf( "\nThe strings ending with \"ED\" are:\n" );
 }
 /* loop through 5 strings */
 for ( i = 0; i <= 4; i++ ) {

 /* find length of current string */
length = strlen( &array[ i ][ 0 ] );

 /* print string if it ends with "ED" */
 if ( strcmp( &array[ i ][ length - 2 ], "ED" ) == 0 ) {
printf( "%s\n", &array[ i ][ 0 ] );

fclose (fileptr);


} /* end if */

}//end for //

} //end else//
 
 return 0; 

} //end main//


Is This A Good Question/Topic? 0
  • +

Replies To: Open file to read, compare string array, print back to the file.

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3028
  • View blog
  • Posts: 10,548
  • Joined: 08-August 08

Re: Open file to read, compare string array, print back to the file.

Posted 14 October 2012 - 02:38 PM

Here's a partially complete version using C++. You should be able to do the same using C:
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

void find_ed_words(vector <string> &words, string &thefile)
{
	ifstream datafile;
	string a_line, a_word;
	std::stringstream ss;
	
	datafile.open(thefile, ifstream::in);

	while (datafile.good()) {
		std::getline(datafile, a_line, '\r');
		ss << a_line;
		while(ss >> a_word) {
			cout << a_word << endl;
			if (a_word.substr(a_word.length()-2,2) == "ed") {
				words.push_back(a_word);
			}
		}
	}
}

void append_to_file(vector <string> &words, string &thefile)
{
		// You can finish this.
}

int main()
{
	string filename = "testfile.txt";
	vector <string> edwords;
	find_ed_words(edwords, filename);
	long i;
	cout << "\nEd words:\n";
	for (i = 0; i < edwords.size(); i++) { // Just so you can see the words have been read and parsed.
		cout << edwords[i] << endl;
	}
	append_to_file(edwords, filename);
	
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#3 smichels  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-October 12

Re: Open file to read, compare string array, print back to the file.

Posted 14 October 2012 - 08:49 PM

Thank you for the snippet, but can you shed more guidance on the contents I am supposed to enter for the second function that appends output? Appreciate your help.
Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1086
  • View blog
  • Posts: 4,556
  • Joined: 09-June 09

Re: Open file to read, compare string array, print back to the file.

Posted 14 October 2012 - 08:57 PM

Focus on writing a function to test whether the a ends with "ed". Once you have that written, it becomes a basic file IO problem.

I'll give you hint
int ends_ed(char *word) {
   int len = strlen(word);
   //check to see if the last letter is 'd'
       //if true, check to see if the letter before is 'e'
          // if true, return true

   // otherwise return false
}


Was This Post Helpful? 0
  • +
  • -

#5 smichels  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-October 12

Re: Open file to read, compare string array, print back to the file.

Posted 14 October 2012 - 09:07 PM

I appreciate your hint----I'll work at this and get back.


View Postjjl, on 14 October 2012 - 08:57 PM, said:

Focus on writing a function to test whether the a ends with "ed". Once you have that written, it becomes a basic file IO problem.

I'll give you hint
int ends_ed(char *word) {
   int len = strlen(word);
   //check to see if the last letter is 'd'
       //if true, check to see if the letter before is 'e'
          // if true, return true

   // otherwise return false
}


Was This Post Helpful? 0
  • +
  • -

#6 smichels  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-October 12

Re: Open file to read, compare string array, print back to the file.

Posted 14 October 2012 - 09:28 PM

Hi again, I worked at it a little bit but am still a bit out.
Could you help me clean this up a bit with explanation?

void append_to_file(vector <string> &words, string &thefile)
{
	int ends_ed(char *word);
    int len;
	int i;

	
 for ( i = 0; i <= 10; i++ ) {

/* find length of current string */
len = strlen( &words[ i ][ 0 ] );
/* print string if it ends with "ED" */
if ( strcmp( &words[ i ][ len - 2 ], "ED" ) == 0 ) {
cout << &words[ i ][ 0 ];
} /* end if */
}

}


Was This Post Helpful? 0
  • +
  • -

#7 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1703
  • View blog
  • Posts: 3,262
  • Joined: 30-May 10

Re: Open file to read, compare string array, print back to the file.

Posted 14 October 2012 - 10:45 PM

A lesson in the value of indentation.
#include <stdio.h>
#include <string.h>

int main(void)
{
  int i;                        /* loop counter */
  int length;                   /* length of current string */
  char array[5][20] = { 0 };    /* 5 strings from user */
  char filename[100];
  FILE *fileptr;

  printf("Please enter the file to open:");
  scanf_s("%s", filename);

  fileptr = fopen(filename, "r");

  if (fileptr == NULL) {
    printf("Unable to open '%s' for input. \n", filename);
  }

  else {
    while (!feof(fileptr))
      /* read in 5 strings from user */
      for (i = 0; i <= 4; i++) {
        printf("\nThe strings ending with \"ED\" are:\n");
      }

    /* loop through 5 strings */
    for (i = 0; i <= 4; i++) {
      /* find length of current string */
      length = strlen(&array[i][0]);

      /* print string if it ends with "ED" */
      if (strcmp(&array[i][length - 2], "ED") == 0) {
        printf("%s\n", &array[i][0]);
        fclose(fileptr);
      }   /* end if */
    }     //end for //
  }       //end else//

  return 0;
}  //end main//



22 while (!feof(fileptr))
23 /* read in 5 strings from user */
24 for (i = 0; i <= 4; i++) {
25 printf("\nThe strings ending with \"ED\" are:\n");
26 }
These are the only lines which execute inside the while loop - because you missed a pair of { }
Also, because there are no file reading statements within the loop, it's never going to reach end of file, so all you see is printfs
See also http://sourceforge.n....php?title=Feof

33 /* print string if it ends with "ED" */
34 if (strcmp(&array[i][length - 2], "ED") == 0) {
35 printf("%s\n", &array[i][0]);
36 fclose(fileptr);
37 } /* end if */
Why are you repeatedly closing the file, only when you matched a word ending in ED.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1