не могу найти ошибку
Код:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
main()
{
FILE* pFile;
char* bufer;
char* bseek;
char* endptr;
int i=0;
int max_size_mass,max_size;
int j=0;
int element;
if((pFile=fopen("11.txt", "rt"))==NULL)
{
printf("\n!Error into the file name!\n");
return 0;
}
fseek(pFile, 0, SEEK_SET);
while((bufer[i]=getc(pFile))!=EOF) i++;
fclose(pFile);
i=0;
bseek = bufer;
while(1)
{
element = strtol(bseek, &endptr, 0);
if( endptr == bseek )
break;
if(*endptr == '\0')
break; //parsing finished
j++;
if( *endptr == '\n'||*endptr == '\r' )
{
j=max_size;
if(max_size>max_size_mass)
{
max_size_mass=max_size;
}
j=0;
}
}
bseek = endptr + 1;
printf("\n%d",max_size_mass);
#include<string.h>
#include<stdlib.h>
main()
{
FILE* pFile;
char* bufer;
char* bseek;
char* endptr;
int i=0;
int max_size_mass,max_size;
int j=0;
int element;
if((pFile=fopen("11.txt", "rt"))==NULL)
{
printf("\n!Error into the file name!\n");
return 0;
}
fseek(pFile, 0, SEEK_SET);
while((bufer[i]=getc(pFile))!=EOF) i++;
fclose(pFile);
i=0;
bseek = bufer;
while(1)
{
element = strtol(bseek, &endptr, 0);
if( endptr == bseek )
break;
if(*endptr == '\0')
break; //parsing finished
j++;
if( *endptr == '\n'||*endptr == '\r' )
{
j=max_size;
if(max_size>max_size_mass)
{
max_size_mass=max_size;
}
j=0;
}
}
bseek = endptr + 1;
printf("\n%d",max_size_mass);
З.Ы: Извините за нестриктурированность кода.
Какой IDE Вы пользуетесь?
Код:
// matrix4.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
using namespace std;
vector<pair<vector<int>,int>> matrix;
int _tmain(int argc, _TCHAR* argv[])
{
FILE* fp = NULL;
char filename[256] = "d:\\matrix.txt";
if ((fp = fopen(filename,"r+")) != NULL)
{
char* pszbuf = new char[1024];
memset((void*)pszbuf, 0x00, 1024);
while (fgets(pszbuf, 1024, fp) != NULL)
{
vector<int> v; int count = 0;
char* psznum = strtok(pszbuf," ");
if (psznum != NULL)
{
while (psznum != NULL)
{
v.push_back(atoi(psznum));
psznum = strtok(NULL," ");
count++;
}
}
matrix.push_back(make_pair(v,count));
}
fprintf(fp,"\n\n");
for (int index = 0; index < matrix.size(); index++)
{
vector<int>::iterator iter;
for (iter = matrix[index].first.begin();
iter != matrix[index].first.end(); iter++)
fprintf(fp,"%d ",*iter);
fprintf(fp,"count = %d\n",matrix[index].second);
}
fprintf(fp,"\n\n");
vector<pair<vector<int>,int>>::iterator iter,
iterMax = matrix.begin();
for (iter = matrix.begin(); iter != matrix.end(); iter++)
if (iterMax->second < iter->second)
iterMax = iter;
vector<int>::iterator iter2;
for (iter2 = iterMax->first.begin();
iter2 != iterMax->first.end(); iter2++)
fprintf(fp,"%d ",*iter2);
fprintf(fp,"count = %d\n",iterMax->second);
}
else
{
cout << "Error opening file " << filename << endl;
}
return 0;
}
//
#include "stdafx.h"
using namespace std;
vector<pair<vector<int>,int>> matrix;
int _tmain(int argc, _TCHAR* argv[])
{
FILE* fp = NULL;
char filename[256] = "d:\\matrix.txt";
if ((fp = fopen(filename,"r+")) != NULL)
{
char* pszbuf = new char[1024];
memset((void*)pszbuf, 0x00, 1024);
while (fgets(pszbuf, 1024, fp) != NULL)
{
vector<int> v; int count = 0;
char* psznum = strtok(pszbuf," ");
if (psznum != NULL)
{
while (psznum != NULL)
{
v.push_back(atoi(psznum));
psznum = strtok(NULL," ");
count++;
}
}
matrix.push_back(make_pair(v,count));
}
fprintf(fp,"\n\n");
for (int index = 0; index < matrix.size(); index++)
{
vector<int>::iterator iter;
for (iter = matrix[index].first.begin();
iter != matrix[index].first.end(); iter++)
fprintf(fp,"%d ",*iter);
fprintf(fp,"count = %d\n",matrix[index].second);
}
fprintf(fp,"\n\n");
vector<pair<vector<int>,int>>::iterator iter,
iterMax = matrix.begin();
for (iter = matrix.begin(); iter != matrix.end(); iter++)
if (iterMax->second < iter->second)
iterMax = iter;
vector<int>::iterator iter2;
for (iter2 = iterMax->first.begin();
iter2 != iterMax->first.end(); iter2++)
fprintf(fp,"%d ",*iter2);
fprintf(fp,"count = %d\n",iterMax->second);
}
else
{
cout << "Error opening file " << filename << endl;
}
return 0;
}
2 5 7 6 4
1 9 2 8 3 6 4 3 1 2 0
7 4 3 9 2 1 2 5
2 8 6
9 7 1 4 4
Результат записывается в конец файла matrix.txt
2 5 7 6 4
1 9 2 8 3 6 4 3 1 2 0
7 4 3 9 2 1 2 5
2 8 6
9 7 1 4 4
2 5 7 6 4 count = 5
1 9 2 8 3 6 4 3 1 2 0 count = 11
7 4 3 9 2 1 2 5 count = 8
2 8 6 count = 3
9 7 1 4 4 count = 5
1 9 2 8 3 6 4 3 1 2 0 count = 11