|
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
|
|
|
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);
}
////////////////////////////////////////
|
|
|
|
|
|
|
|