Connecting Tech Pros Worldwide Help | Site Map

cant find the problem with my text file

Newbie
 
Join Date: Dec 2006
Posts: 7
#1: Dec 13 '06
I am trying to answer a question with regards to entering a set of numbers and text, and then I have got to search for a number and the text with it.

I wish to enter the name on one line and the number on another line

This is the code that I have written for inputting the numbers:

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <stdio.h>
using namespace std;

int main()
{
char name[20];
int mark;
int x;
FILE *f;
f = fopen("black.txt","w");
for(x=1; x<=5; x++)
{
printf("Enter a name \n");
gets(name);
printf("Enter a mark \n");
scanf("%d",&mark);
fprintf(f,"%s %d\n",name, mark);
}

fclose(f);
return 0;
}

it will not input the number and mark correctly together

the other part of the code to display a certain number and text is :


#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <stdio.h>
using namespace std;

int main()
{
char name[20];
int number;
int x;
FILE *f;
f = fopen("black.txt","w");
while ( fscanf(f,”%s”,name )
{
if (number=10)
{
printf(“%s \n”,name);
}
}
fclose(f);
return 0;
}

this part is not working properly either and I am unable to spot what is wrong with it.
so if someone can help me that would be great.
Newbie
 
Join Date: Nov 2006
Location: Hyderabad India
Posts: 15
#2: Dec 13 '06

re: cant find the problem with my text file


this part is not working properly either and I am unable to spot what is wrong with it.
so if someone can help me that would be great.[/quote]

Dude,

Here is the corrected code. Please go thro. I dont want to (too lazy to) tell where i did the changes.. you will find them anyways :)


#include <iostream>
#include <fstream>
#include <stdio.h>
using namespace std;

int main()
{
char name[20];
int mark;
int x;
FILE *f;
f = fopen("c:/black.txt","w");
for(x=1; x<=10; x++)
{
printf("Enter a name \n");
scanf("%s",name);
printf("Enter a mark \n");
scanf("%d",&mark);
fprintf(f,"%s %d\n",name, mark);

}

fclose(f);
return 0;
}



include <iostream>
#include <fstream>
#include <stdio.h>
using namespace std;

int main()
{
char name[20];
int number;
int x;
FILE *f;
f = fopen("c:/black.txt","r");
while ( fscanf(f,"%s %d",name,&number) != EOF )
{
if (number==10)
{
printf("%s \n",name);
}
}
fclose(f);
return 0;
}


Regards,
ShaggY@FtF
Newbie
 
Join Date: Dec 2006
Posts: 7
#3: Dec 14 '06

re: cant find the problem with my text file


thanx for the help with that, it work great now, but i was wondering if you know how to read the names and numbers from the file into an array, if so agin that would be a great help. becasue i just cant get my head around how to do it.
Reply