Also in the same program :
Code:
// ozumsafaoglu.cpp : Defines the entry point for the console application.
//
#include "stdio.h"
#include "stdafx.h"
#include "stdlib.h"
int _tmain(int argc, _TCHAR* argv[])
{
struct section {
char first[30];
char second[30];
int secno;
};
int totalline=0; // To find the total section number //
char a[40]; // To take each line of the txt to find the total section number //
FILE *fp;
if((fp=fopen("liste.txt","r"))==NULL) {
perror("liste.txt");
}
else {
while((fgets(a,sizeof(a)-1,fp))!=NULL)
{
totalline+=1;
}
struct section sections[(totalline+1)/3]; // in order to provide minimum memory spent //
rewind(fp);
int i=0; // will be in for loop in order to control line number
for(i=0;i<totalline;i++) {
if( i % 3==0) {
sections[i].secno= i/3 + 1;
fgets(sections[i/3+1].first,29,fp);
fgets(sections[i/3+1].second,29,fp);
fgets(a,sizeof(a)-1,fp);
}
}
for(i=0;i<totalline;i++) {
printf("section %d\n",sections[i].secno);
printf("%s\n%s\n\n",sections[i].first,sections[i].second);
}
}
return 0;
}
Error messages :
1>------ Build started: Project: ozumsafaoglu, Configuration: Debug Win32 ------
1>Compiling...
1>ozumsafaoglu.cpp
1>e:\documents\visual studio 2005\projects\ozumsafaoglu\ozumsafaoglu\ozumsafaog lu.cpp(17) : warning C4996: 'fopen' was declared deprecated
1> d:\program files\microsoft visual studio 8\vc\include\stdio.h(234) : see declaration of 'fopen'
1> Message: 'This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.'
1>e:\documents\visual studio 2005\projects\ozumsafaoglu\ozumsafaoglu\ozumsafaog lu.cpp(26) : error C2057: expected constant expression
1>e:\documents\visual studio 2005\projects\ozumsafaoglu\ozumsafaoglu\ozumsafaog lu.cpp(26) : error C2466: cannot allocate an array of constant size 0
1>e:\documents\visual studio 2005\projects\ozumsafaoglu\ozumsafaoglu\ozumsafaog lu.cpp(26) : error C2133: 'sections' : unknown size
1>Build log was saved at "file://e:\Documents\Visual Studio 2005\Projects\ozumsafaoglu\ozumsafaoglu\Debug\Buil dLog.htm"
1>ozumsafaoglu - 3 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
-----------------------------
The aim of my program :
There is a text file :
Tuesday 15:40 17:30 F19
Thursday 15:40 17:30 F17
Tuesday 15:40 17:30 FZ19
Thursday 12:40 14:30 F19
Tuesday 15:40 17:30 F15
Thursday 12:40 14:30 FZ19
Monday 10:40 12:30 F19
Thursday 12:40 14:30 F15
My program takes each pairs of line in individual sturct variables. That moduler of 3 is used for this aim.Than I will assign section number to each of them and print them on the screen with section numbers. I am trying to achive taking them into struct successfully.