i have a delimited file it has

name|address|address2|phone number/n

i found this code snippet of code which i modfied to almost do what i want. the problem is i want to only count the delimited char which is '|" and '\n' but the program is counting the char instead. here is the code it is finding the first record. the program is finding records and displaying length of field i want to change program to determine record count by '\n' and display record.

#include <stdio.h>
#include <iostream>


using namespace std;

#define SIZE 30

int main()
{

  char data[SIZE];

  int inchar;
  FILE *infile;
  char *inname = "phonebook.dat";

  
  
  

  int i;
  infile = fopen(inname, "r");
  


  
  if (!infile) {
    printf("Couldn't open %s for reading\n");
    return 0;
  }
 for (int count=0; count < 4; count++)
 {
  for (i = 0; i < SIZE - 1; i++) {
   inchar = fgetc(infile); 
    
	if ((inchar == '|') || (inchar=='\n')) {
      printf("%d: char count encountered\n", i); /* print is for debug purposes only */
	  break;
    }
  
    else {
      data[i] = inchar;
	  
    }
  }
 
  data[i] = '\0';

  printf("data: <%s>\n", data);
 
 }

  return 0;
}

i need help in modifing code to count the number of records the '\n' will be one record. i need program to determine how many records in file so i can loop until eof to get all records.

Could you please post using code tags?
And by the way: your code is still using the C file handling this isn't wrong but in C++ the C++ way for File I/O is preferred :)

i need help in modifing code to count the number of records the '\n' will be one record. i need program to determine how many records in file so i can loop until eof to get all records.

So you mean that you've one record per line in the file ?

commented: *nods* +19

Declare a new variable in your code called d_count (delimiter count) for example ( int d_count=0; )
(Remember to do this outside your for-loop).
Change this piece of code (the changed part is marked in green):

if ((inchar == '|') || (inchar=='\n')) {
printf("%d: chars counted\n", [B]++d_count[/B]);
break;
}

What you were doing is just displaying the loop's index ( i ) which is wrong of course, because this index is incremented always when a character is read :)

thanks for helping me. i did the changes you suggested but still having a problem where to stick loop til end of file .
i need a little time getting rid of c style code when i try to replace it with ifstream i get this error : error C2040: 'infile' : 'std::ifstream' differs in levels of indirection from 'FILE *'

i added a loop and can display all records but having a hard time to automate it i set count < 8 knowing i only have two records now. i need to determine how many records by itself. here is my output as you can see i added d_limit.
field:0 fieldlength:10 <james bond>
field:1 fieldlength:13 <777 spy drive>
field:2 fieldlength:16 <holywood, ca 007>
field:3 fieldlength:8 <555-1234>
field:4 fieldlength:10 <james west>
field:5 fieldlength:15 <123 train drive>
field:6 fieldlength:17 <holywood, ca 9999>
field:7 fieldlength:8 <555-9876>

when i try to use while(!infile.eof()) i get two error's
(24) : warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

(32) : error C2228: left of '.eof' must have class/struct/union

well I'm not to sure about FILE but i know that if you do this you can loop through the file until the end.

ifstream fin(*infile);
while (!fin.eof())
{
for (int count=0; count < 4; count++) 
{  
for (i = 0; i < SIZE - 1; i++)
{
fin >> inchar
//...

NathanOliver, your code won't work !!

I would use a function like getline to do the delimiting for me.
I'd use a nested loop. The outer loop to read the file one line at a time (so delimit on the '\n') and one loop to delimit each line on a '|'.
Now all I have to do is add two counters to count have many times each situation occured.

std::ifstream infile("c:/example.txt");
    if (!infile.is_open() || !infile.good()){
        std::cout << "Failed to open file!";
        return -1;
    }
    std::string line = "", word = "";
    int linecount = 1, wordcount = 1;
    while (std::getline(infile,line)){
        std::stringstream sstr(line);
        while (std::getline(sstr, word, '|')){
            std::cout << "word: " << word << " is word " << wordcount << " on line " << linecount << "\n";
            wordcount++;
        }
        wordcount = 1;
        linecount++;
    }

Yup, using getline is probably the easiest way to achieve this :)

sorry i meant to do this

char ch;
ifstream fin(*infile);while (!fin.eof()){for (int count=0; count < 4; count++) {  for (i = 0; i < SIZE - 1; i++){fin >> inchar//...ifstream fin(*infile);
while (!fin.eof())
{
for (int count=0; count < 4; count++) 
{  
for (i = 0; i < SIZE - 1; i++)
{
fin.get(ch);
//...

I'm pretty sure that will work.

i sovled the loop problem and yes i will look at getline but this was interesting here's how i sovled loop

#include <stdio.h>
#include <iostream>
#include <fstream>


using namespace std;
int num = 4;
int* iPtr = &num;


#define SIZE 30

int main()
{

  char data[SIZE];
  bool endoffile="false";
  int inchar,d_limit=0,recordcount=0,count=0;
  FILE *infile;
  char *inname = "phonebook.dat";
  

  
  
  

  int i;
  infile = fopen(inname, "r");
  


  
  if (!infile) {
    printf("Couldn't open %s for reading\n");
    return 0;
  }
 for (int count=0; count < num;count++)
 {
  for (i = 0; i < SIZE - 1; i++)
  {
   inchar = fgetc(infile);
   if (inchar=='\n')
   {
		*iPtr = num + 4;
   }
   if (inchar==-1)
   {
	   return 0;
   }
  
	if ((inchar=='|') || (inchar=='\n'))
		
	 {
		
		printf("field:%d ", ++d_limit);
		printf("fieldlength:%d\n", i); /* print is for debug purposes only */
	  break;
	}
  
    else 
	{
      data[i] = inchar;
	  
    }
  
  }

  
 
  data[i] = '\0';

  printf("data: <%s>\n", data);
 }
return 0;
	
  
}

i have a search example but it display's whole line i would like to display each field

here is output :

field:1 fieldlength:10
data: <james bond>
field:2 fieldlength:13
data: <777 spy drive>
field:3 fieldlength:16
data: <holywood, ca 007>
field:4 fieldlength:8
data: <555-1234>
field:5 fieldlength:10
data: <james west>
field:6 fieldlength:15
data: <123 train drive>
field:7 fieldlength:17
data: <holywood, ca 9999>
field:8 fieldlength:8
data: <555-9876>

i will mark as sovled after a little while to see comment's . i only took one c++ course i had to read on pointer's again. i hope i did tag right on code posted.

>i hope i did tag right on code posted.
Alas, correct tags are:
[code=cplusplus] source

[/code]

>i hope i did tag right on code posted.
Alas, correct tags are:
[code=cplusplus] source

[/code]

But you did get the CODE tags correct. The cplusplus part is optional.

Also, the PREVIEW button would show you if the tags are correct.

thanks niek_e that is awesome now i want to search for let say james and give me all the james in phonebook.
i really like what you did.

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.