codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  reading text files line by line  urosh at 10:37 on Friday, July 07, 2006
 

hi

i need code which will read numbers from txt file. number of numebrs in line is not fixed so i need to detect end of line somehow. for example, if we have file which looks like this:

12 32 54 67 88 90
12 34 55
22 45 66 78

first array shuold contain 6, second 3 thirs 4 elements.

problem i have is that i cant detect end of line.

can anyine help me with this?

thnx

  Re: reading text files line by line  Gord T at 14:42 on Monday, November 20, 2006
 

This program will do what you are asking. It was written and tested with Visual Studio 2005, c++ Win32 console application.
It assumes your numbers are whole numbers and do not contain decimals although it could be modified
for that. The numbers are just output to screen with provisions to write your own code to send the numbers where you want(multi-dimension arrays, ect).
It also assumes the value of the numbers in the text file can be converted to a ‘long’, else you will need some error checking code to look for that.


/////////////////////////////////////////////////
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#pragma once


#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <stdio.h>
#include <tchar.h>


/////////////////////////////////////////////////
//ReadText.h
#pragma once


void RunProgram();
///////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////
//ReadText.cpp
#include "stdafx.h"
#include <malloc.h>
#include <memory.h>
#include <stdlib.h>
#include "ReadText.h"

void ReadFile(char *fn);

#define INVALID_NUMBER 0
#define VALID_NUMBER 1
#define NEW_LINE 2

//
// RunProgram() -the program.
//
void RunProgram()
{
ReadFile("/forum/Ctemptest.txt");
}
//
// GetNextNumber -picks off the numbers, ignore all else except carriage return.
//
long GetNextNumber(long * next_number,char *type,int start,int max,char * buffer)
{
long j,k;
const long SIZE_NUMBER = 16;
char number[SIZE_NUMBER + 1];

*type = INVALID_NUMBER;

//remove leading non-numbers.
for(j = start; j < max; j++)
{
if( (buffer[j] >= '0') && (buffer[j] <= '9')) break;//is a number

if(buffer[j] == 13)// new line
{ *type = NEW_LINE;
return (j+1);
}
}

//read in number
k = 0;
for( ; j < max; j++)
{
if( (buffer[j] < '0') || (buffer[j] > '9')) break;
if( k >= (SIZE_NUMBER -1)) break; //number toooo big.
number[k] = buffer[j];
k++;
}

//convert string to long
number[k] = 0;
*next_number = atol(number);

if(k) {*type = VALID_NUMBER;}

return (j);
}
//
// UpdateDataBase -this contains one complete line of numbers.
// -Add your code here.
//
void UpdateDataBase(long *numbers, long num_numbers)
{
long j;

for(j = 0; j < num_numbers; j++)
{printf("%d ",numbers[j]);}

printf("\r\n");
}

//
// ReadFile -reads file into buffer and processes the data.
// Numbers in file must be ascii 'long's, not float's or doubles. Else modify code.
//
void ReadFile(char *fn)
{
FILE *stream = 0;
errno_t err;
long max_bytes, num_read;
char *buffer = (char *)0;
char temp;

// open file in read-binary mode
err = fopen_s(&stream,fn, "rb");
if(err !=0 )
{ printf("Error opening file <%s>\r\n",fn);
return;
}

// get length of file
max_bytes = 0;
while( fread( &temp,1,1,stream ) != 0) {max_bytes++;}

//allocate for array and read in all bytes.
buffer = (char*)malloc(max_bytes);
fseek(stream,0,SEEK_SET);
num_read = (int)fread( buffer,1,max_bytes,stream );
fclose( stream );

// read all of the numbers and look for returns.
long j;
long next_number;
char type;
const long MAX_NUMBERS_PER_LINE = 160;
long * numbers;
long num_numbers, num_lines;

numbers = (long*)malloc(MAX_NUMBERS_PER_LINE * sizeof(long));

j = 0;
num_numbers = 0;
num_lines = 0;
while(j < max_bytes )
{
j = GetNextNumber(&next_number,&type,j,max_bytes,buffer);
if(j >= (max_bytes - 1)) break;

if(type == VALID_NUMBER) //store numbers
{
if(num_numbers >= (MAX_NUMBERS_PER_LINE-1))
{num_numbers = MAX_NUMBERS_PER_LINE -1;}

numbers[num_numbers] = next_number;
num_numbers++;
}
else if (type == NEW_LINE)
{
UpdateDataBase(numbers,num_numbers);
num_numbers = 0;
num_lines++;
}

}

if(num_numbers)
//missed last line, did not contain new-line character.
{
UpdateDataBase(numbers,num_numbers);
num_numbers = 0;
num_lines++;

}
printf("\r\nFinished %d lines.\r\n", num_lines);

free(numbers);
free(buffer);

}
////////////////////////////////////////









CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  AD Skyscrapers
•  Re: Using a Pipe | with a Variable Name!
•  adding audio to html page
•  How to web data base?
•  Re: For better design, please vote!
•  Where jpeg, tiff conventions?
•  Re: Looking to code something.
•  Re: Trying to create a C++ Object Oriented game
•  cant get iframe1 links to load into frame2 HELP please?


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2006