finding string from a text file

Hi,
I currently have a program that is able to write a string of characters(serial number) into a txt file.
The program will then read from the file and create another text file for printing.
I am trying to edit my program for tracking purpose, so there will be no chances of double printing. The ideal way will be creating a database and searching for it. However, it is too complex for me.
I am thinking of just using and searching through the text file for duplicates. ie once a serial number has been saved into a text file, any duplicate found will be detected and the program will prompt.
I need some help/example for searching through the text file for the serial number and prompt if it is duplicate.
Some part of my program is as follows. It is quite a lengthy code and it should be shorter but I am a C++ beginner.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
						if ( strncmp (type_serial,"0",1) == 0 )// product is 1188 AC
					{	cout<<"Product is 1188AC.\n\n";
						memmove(type_serial, type_serial+5, strlen(type_serial)); //Remove next 5 numbers(total 8), get last number
					if ( strncmp (type_serial,"4",1) == 0 )// product is produced at FAT
						{	//cout<<"Production location is correct\n";
							database_numbers_textfile= fopen("1188AC Database Numbers.TXT","a");
							fprintf(database_numbers_textfile, "%s\n",scan_dhr);
							fclose(database_numbers_textfile);
							print_file_read= fopen("Scanned Numbers.TXT","r");
							if (print_file_read == NULL)	{	cout<<"Error! Cannot open Scanned Numbers.TXT";	return 0;	}
							print_file_write= fopen("1188AC.TXT","w");
							if (print_file_write == NULL)	{	cout<<"Error! Cannot open 1188AC.TXT";	return 0;	}
								do{	bartender_commander=fgetc(print_file_read);
									if (bartender_commander!=EOF)
										{	fputc(bartender_commander, print_file_write);	}
								}while (bartender_commander !=EOF);
							fclose(print_file_read);
							fclose(print_file_write);
							cout<<"Printing...\n\n";
goto main_menu_selection_case3_option1;
						}					
					else //wrong production location
					cout<<"\a\a\aProduction location is WRONG!!!\n";
goto main_menu_selection_case3_option1;
					}
Well you say beginner but there are lots of functions and commands in here that i don't understand!

However thinking of the database thing I suppose you could create the serial code one on each line, then on application launch it reads the file where they are stored and set pointer to ((ios::end)-SerialSize).
Here SerialSize is the size of the code you're storing (i assume they are all same length) and so this will set the pointer to the start of the last line...

Then you can simply read that bottem line to get your starting point and +1 for the next code (or whatever you do to increment code)
Hi recca85,

I see goto's in your code: -this is usually a bad idea, because it leads to spaghetti code.

Some good advice given by experienced programmers is (vlad from moscow is one)to: "Forget that the language has the capability of goto's"

Code can be written using loops (for, while or do). Also use functions to modularise the code.

There are STL algorithms that can search for strings. Basically use getline to read a line from file into a string. Use find algorithm to search for your string in this line-string.

Hope this helps
Hi, the codes i used were copied and modified from examples found in the net. It took me a while to understand. By right, I should masked the serial numbers but I just can't find it, so I did a copy of the serial number and remove the memory one by one for my comparison.

The database numbers are appended. I see them are stored line by line automatically.
My serial numbers are of same length, 9 characters (12A456789). So is the size 9?
Are you beginning in C or C++?

I ask because memmove, fclose, fprintf, strncmp, fgetc and fputc are C library methods.

You are free to mix and match at your pleasure but you might want to understand the differences between C and C++ as you go along so you don't get their libraries mixed up.


EDIT - This is actually a C program that uses cout statements.
That's why Satsumi was confused. lol
Last edited on
Erhmm, It's Satsuma :D
And yes i had a suspision that this was fairly backdated code (although C++ is designed to work with it's original C libaries) however i am a learner to C++ (getting to a fair standing with language now) so i don't know much about the original language and for now i'm not that curious 'cos i dont want to get muddled up.
Topic archived. No new replies allowed.