473,387 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

cant find the problem with my text file

7
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.
Dec 13 '06 #1
2 1338
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
Dec 13 '06 #2
cv87cv
7
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.
Dec 14 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Tiernan | last post by:
Hey everybody. I'm verry new to PHP and MYSQL and have been working on a form that when it is submitted stores the information into a mysql database. The main problem is that i'm trying to finish...
1
by: pmud | last post by:
In my ASP.Net application I added a required field validator.Before adding this , my application was running fine but after ading this, when i tried to view the .aspx in browser, i got an alert msg...
4
by: Jake Lewis | last post by:
I have an HTML page that loads fine including the .js file <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Untitled Document</title> <script...
0
by: vinidimple | last post by:
Hi i have a serious problem while i was working in Excel.I want to fetch columns from an excel worksheet and i need to compare it with an sql querry fields,so i tried to open an excle...
19
by: bb nicole | last post by:
Below is my search engine for job portal which jobseeker can find the job through quick search. But it cant work... Is it mysql query got problem?? Thanx.. Interface <html> <head> <title>UMS...
2
by: andrewanderson | last post by:
hi can anyone help me with this prog. cant find the prob why it cant display cout<<"This is the display of your transaction"<<endl; ifstream fobj; //declare input file stream ...
12
by: lalou89 | last post by:
Develop a simple text editor program. The program will show the user a menu of choices and will act according to his choice. Use functional decomposition to break the system into small functions that...
6
by: WolfgangS | last post by:
Ok first off, i am a total beginner at this groups stuff and i have no clue how this works. This is probabaly the wrong group for my problem but i will post it anyways. Learning by doing right? ...
66
by: happyse27 | last post by:
Hi All, my html code is sno 1) and perl code is sno 2). a) I tried to print $filename and it cant print out the value, only blank was displayed, and the file could not be uploaded. And it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.