Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[SOLVED] Working With Data Files In C

form

  • This topic is locked This topic is locked
16 replies to this topic

#1 Agent001

Agent001

    CC Regular

  • Member
  • PipPipPip
  • 41 posts

Posted 27 May 2012 - 10:18 AM

Hi guys, i need your help once again, this time working with data (txt) files in C.

If i understand well (i'm working with Visual Studio 2008), i need to define command line arguments - meaning that i need to define what the program will get as it's arguments via command line?!
Also, i know it is done in Project Properties -> Debugging -> Command Arguments

So, i have two .txt files - one which will be read, and other which will be written into, and a certain float number - what is the form of command line arguments - ie do i enter file1.txt file2.txt 15.25 or what? in the command arguments field or what?

The reason i'm asking this is i get an error - debug assertion failed - and i'm guessing it has something to do with the form of data i entered in command arguments?
  • 0

#2 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 27 May 2012 - 10:32 AM

You should use absolute path in the command line with double quote ("") for string arguments. You can use the following string as command line arguments to your program. Note that even if you provide float argument, you will get it as string. So you need to use atof to convert string to float number.

Arguments: "D:\Workspace\Input.txt" "D:\Workspace\out.txt" 12.7

char *a = argv[1];
char *b = argv[2];
char *c = argv[3];
float f = atof(c);

  • 0

#3 Agent001

Agent001

    CC Regular

  • Member
  • PipPipPip
  • 41 posts

Posted 27 May 2012 - 10:47 AM

Hm, i still get debug assertion file, i can't localize the error

Can you take a look at my code?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

#define MAX_LEN 100
#define NO_MEMORY "Nema dovoljno memorije"
#define FPS_NUM 25

typedef struct vreme{
	int sat;
	int min;
	int s;
	int ms;
} VremeTip;

typedef struct podatakStruktura{
	VremeTip pojavljivanje;
	VremeTip uklanjanje;
	char tekst[MAX_LEN];
} PodatakTip;

typedef struct cvorListe{
	struct cvorListe *prethodniEl;
	struct cvorListe *sledeciEl;
	PodatakTip *podatak;
} CvorListeTip;

VremeTip izFrameUVreme(int frame){
	VremeTip vreme;
	int tmp;
	float ukupnoVremeSaDecimalom;
	int ukupnoVreme;
	ukupnoVremeSaDecimalom = ((float)frame)/FPS_NUM;
	if((ukupnoVremeSaDecimalom - (int)ukupnoVremeSaDecimalom)==0)
		vreme.ms = 0;
	else
		vreme.ms = (ukupnoVremeSaDecimalom - (int)ukupnoVremeSaDecimalom)*1000;
	ukupnoVreme = ukupnoVremeSaDecimalom;
	vreme.s = ukupnoVreme % 60;
	ukupnoVreme -= vreme.s;
	ukupnoVreme /= 60;
	vreme.min = ukupnoVreme % 60;
	ukupnoVreme -= vreme.min;
	vreme.sat = ukupnoVreme / 60;
	return vreme;
}

void izDatUListu(char *imeDat, CvorListeTip **lista){	//elementi se dodaju na kraj
	FILE *dat;
	char c;
	int frame, i;
	VremeTip vreme;
	CvorListeTip *trenutni, *novi;
	PodatakTip *podatak;
	dat = fopen(imeDat, "r");
	trenutni = *lista;
	while((c = fgetc(dat)) != EOF){
		i=0;
		podatak = malloc(sizeof(PodatakTip));
		if(NULL == podatak){
			printf(NO_MEMORY);
			exit(1);
		}
		novi = malloc(sizeof(CvorListeTip));
		if(NULL == novi){
			printf(NO_MEMORY);
			exit(1);
		}
		fscanf(dat, "%d", &frame);
		vreme = izFrameUVreme(frame);
		podatak->pojavljivanje = vreme;
		fgetc(dat);
		fgetc(dat);
		fscanf(dat, "%d", &frame);
		vreme = izFrameUVreme(frame);
		podatak->uklanjanje = vreme;
		fgetc(dat);
		while((c=fgetc(dat)) != '\n' && c != EOF){
			if(c!='|')
				podatak->tekst[i++] = c;
			else
				podatak->tekst[i++] = '\n';
		}
		podatak->tekst[i] = '\0';
		novi->podatak = podatak;
		if(trenutni == NULL){
			novi->prethodniEl = NULL;
			novi->sledeciEl = NULL;
			*lista = novi;
		}
		else{
			novi->sledeciEl = NULL;
			novi->prethodniEl = trenutni;
			trenutni->sledeciEl = novi;
		}
		trenutni = novi;
	}
	fclose(dat);
}

void dodajPomerajElementu(PodatakTip **podatak, int pomeraj){
	VremeTip vreme;
	int tmp;
	vreme.sat = pomeraj / 3600;
	pomeraj -= vreme.sat;
	vreme.min = pomeraj / 60;
	pomeraj -= vreme.min;
	vreme.s = pomeraj;
	vreme.ms = 0;
	(*podatak)->pojavljivanje.s += vreme.s;	//vreme pojavljivanja
	(*podatak)->pojavljivanje.min += vreme.min;
	(*podatak)->pojavljivanje.sat += vreme.sat;
	if((*podatak)->pojavljivanje.s > 59){		// u slucaju da je neka od jedinica presla 60
		tmp = (*podatak)->pojavljivanje.s / 60;
		(*podatak)->pojavljivanje.s -= 60*tmp;
		(*podatak)->pojavljivanje.min += tmp;
	}
	if((*podatak)->pojavljivanje.min > 59){
		tmp = (*podatak)->pojavljivanje.min / 60;
		(*podatak)->pojavljivanje.min -= 60*tmp;
		(*podatak)->pojavljivanje.sat += tmp;
	}
	(*podatak)->uklanjanje.s += vreme.s;
	(*podatak)->uklanjanje.min += vreme.min;
	(*podatak)->uklanjanje.sat += vreme.sat;
	if((*podatak)->uklanjanje.s > 59){		
		tmp = (*podatak)->uklanjanje.s / 60;
		(*podatak)->uklanjanje.s -= 60*tmp;
		(*podatak)->uklanjanje.min += tmp;
	}
	if((*podatak)->uklanjanje.min > 59){
		tmp = (*podatak)->uklanjanje.min / 60;
		(*podatak)->uklanjanje.min -= 60*tmp;
		(*podatak)->uklanjanje.sat += tmp;
	}
}

void dodajPomerajLista(CvorListeTip *lista, int pomeraj){
	CvorListeTip *trenutni;
	trenutni = lista;
	while(trenutni){
		dodajPomerajElementu(&(trenutni->podatak), pomeraj);
		trenutni = trenutni->sledeciEl;
	}
}

void upisiVremeUDat(FILE **dat, VremeTip vreme){
	int i, br;
	char tmp[6];
	itoa(vreme.sat, &tmp, 10);
	br = strlen(tmp);
	if(br<2){
		for(i=0; i<2-br; i++)
			fputc('0', *dat);
	}
	for(i=0; i<br; i++)
		fputc(tmp[i], *dat);
	fputc(':', *dat);
	itoa(vreme.min, &tmp, 10);
	br = strlen(tmp);
	if(br<2){
		for(i=0; i<2-br; i++)
			fputc('0', *dat);
	}
	for(i=0; i<br; i++)
		fputc(tmp[i], *dat);

	fputc(':', *dat);
	itoa(vreme.s, &tmp, 10);
	br = strlen(tmp);
	if(br<2){
		for(i=0; i<2-br; i++)
			fputc('0', *dat);
	}
	for(i=0; i<br; i++)
		fputc(tmp[i], *dat);

	fputc(',', *dat);
	if(vreme.ms == 0)
		fputs("000", *dat);
	else{
		itoa(vreme.ms, &tmp, 10);
		for(i=0; i<strlen(tmp); i++)
			fputc(tmp[i], *dat);
		while(i<3){
			fputc('0', *dat);
			i++;
		}

	}
		
}

void izListeUDat(char *imeDat, CvorListeTip *lista){
	FILE *dat;
	char c;
	CvorListeTip *trenutni;
	int i=1, j;
	dat = fopen(imeDat, "w");
	trenutni = lista;
	while(trenutni){
		j=0;
		fprintf(dat, "%d", i);
		fputc('\n', dat);
		upisiVremeUDat(&dat, trenutni->podatak->pojavljivanje);
		fputs(" --> ", dat);
		upisiVremeUDat(&dat, trenutni->podatak->uklanjanje);
		fputc('\n', dat);
		while(trenutni->podatak->tekst[j] != '\0'){
			if(trenutni->podatak->tekst[j] != '|'){
				fputc(trenutni->podatak->tekst[j++], dat);
			}
			else{
				fputc('\n', dat);
				j++;
			}
		}
		fputc('\n',dat);
		fputc('\n',dat);
		trenutni = trenutni->sledeciEl;
		i++;
	}
	fclose(dat);
}

int main(){
	VremeTip vreme;
	CvorListeTip *lista = NULL;
	izDatUListu("primer.txt", &lista);
	dodajPomerajLista(lista, 0);
	izListeUDat("izlaz.txt", lista);
}


  • 0

#4 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 27 May 2012 - 10:52 AM

Your main definition should be like
int main(int argc, char *argv[]){}

  • 0

#5 Agent001

Agent001

    CC Regular

  • Member
  • PipPipPip
  • 41 posts

Posted 27 May 2012 - 10:54 AM

I changed that, and still get the error!

Have you tried the code?
  • 0

#6 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 27 May 2012 - 10:57 AM

The assertion I got is in line "dat = fopen(imeDat, "r");" It is because the file "primer.txt" is not opened (perhaps the file is not in current directory). Please write absolute path for your files, as example,
izDatUListu("C:\\primer.txt", &lista);

        dodajPomerajLista(lista, 0);

        izListeUDat("C:\\izlaz.txt", lista);

  • 0

#7 Agent001

Agent001

    CC Regular

  • Member
  • PipPipPip
  • 41 posts

Posted 27 May 2012 - 11:05 AM

Hm, i think i changed it everywhere, but i still get the same error?
  • 0

#8 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 27 May 2012 - 11:15 AM

Can you debug the programs? Try to catch your error by debugging.

Another suggestion is check the FILE pointer after each fopen call. Check whether the FILE pointer returned by every fopen call is NUL or not. If not NULL, attempt to read from it, otherwise not.

If you want me to debug your programs, I need your data file as well.
  • 0

#9 Agent001

Agent001

    CC Regular

  • Member
  • PipPipPip
  • 41 posts

Posted 27 May 2012 - 11:36 AM

Well, when i try to debug, it seems it doesn't open the file, but i don't know why.
I tried forwrding the full path, but still nothing?

this line
while((c = fgetc(dat)) != EOF){
causes the program to break.

the entering file should be a .txt title file.

I would be gratefull if you could try and find the error, because obviously i can't.

Here's the entering file

Attached Files


  • 0

#10 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 27 May 2012 - 04:34 PM

I just changed the relative file path to absolute file path in main function as follows and I found it is working for me. Attachment izlaz.txt is the file I got as output after running the program. Here is the main function after modifying.
int main(){
VremeTip vreme;
CvorListeTip *lista = NULL;
izDatUListu("D:\\Downloaded\\example.txt", &lista);
dodajPomerajLista(lista, 0);
izListeUDat("D:\\Downloaded\\izlaz.txt", lista);
}

Attached Files


  • 0

#11 Agent001

Agent001

    CC Regular

  • Member
  • PipPipPip
  • 41 posts

Posted 28 May 2012 - 08:29 AM

It was my bad - I was passing the wrong absolute path :( - i typed '\' instead of '\\'.

Another question - how to i write a float number to a file?
For example, i have 12.511254 and i want to write 12.51 in the txt file?

Do i need to convert the number into a string, and if yes how do i do it? Is there a function like itoa(), just for floats :D
  • 0

#12 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 28 May 2012 - 08:32 AM

You need to use fprintf method.
FILE *filePointer;
fprintf(filePointer, "%.2f", 12.511254f);

  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download