I have a problem, I was trying to make a C program taht read a file with 50 lines and 11 columns, the problem is that this file is completely made of strings and I made this:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
typedef struct
{
char nome[100];
char dica[10][300];
} PAIS;
void main()
{
int i;
i = 0;
PAIS paises[50];
char nome[30];
FILE *arq;
arq = fopen("Dicas3.txt", "r");
//fscanf(arq,"%s", nome);
//printf("%s", nome);
while(!feof(arq))
{
fscanf(arq,"%s %s %s %s %s %s %s %s %s %s %s", paises[i].nome, paises[i].dica[0][0], paises[i].dica[1][0], paises[i].dica[2][0], paises[i].dica[3][0], paises[i].dica[4][0], paises[i].dica[5][0], paises[i].dica[6][0], paises[i].dica[7][0], paises[i].dica[8][0], paises[i].dica[9][0]);
printf("%s %s %s %s %s %s %s %s %s %s %s", paises[i].nome, paises[i].dica[0][0], paises[i].dica[1][0], paises[i].dica[2][0], paises[i].dica[3][0], paises[i].dica[4][0], paises[i].dica[5][0], paises[i].dica[6][0], paises[i].dica[7][0], paises[i].dica[8][0], paises[i].dica[9][0]);
i++;
}
system ("PAUSE");
}
The program compile, but the program dosn´t work. Please, could someone show me how to do a program that read a matrix 50x11 and print in the window this matrix?
the matrix is in this file (is in portuguese, but this matrix is made os strings):
https://drive.google.com/file/d/1BLhGSHIx69Ycrasgtl4lKyxxKPRlSAv2/view?usp=sharing
Dont't use feof() find out reason for Why is “while ( !feof (file) )” always wrong?
If you know well in advance about no of lines and columns, since in each line 11 columns/strings are there, read line by line from file using fgets() instead of fscanf(). My suggestion is instead of local array use dynamic array if you want to make it generic solution..
What nome[] contains, not clear ?
using fscanf() :
typedef struct
{
char nome[100];
char dica[10][300];
} PAIS;
int main()
{
PAIS paises[50];
char nome[30];
FILE *arq;
arq = fopen("Dicas3.txt", "r");
if(arq == 0) {
printf("file not present:\n");
return 0;
}
int i=0,j=0;
char ch;
while(fscanf(arq,"%s",paises[i].dica[j])>0) {
printf("%s ",paises[i].dica[j]);
j++;//column
if((ch = fgetc(arq))=='\n')//when new lines occures, start reading from next lines, do i++
{
i++;//rows or lines
printf("\n");//manuaally put the new line or use fputc(ch,stdout)
fseek(arq, -1, 1);//move one letter back
j=0;
}
else
fseek(arq, -1, 1);//move to exact position
}
return 0;
}
I hope it helps.
Related
I'm having problems using a fscanf function. It never reads it correctly and always results in blanks.
fscanf(f, " %[^;];%[^;];%d;%d;%d;%d;%[^;];%d;%[^\n]",
arr[i].loc1, arr[i].loc2, &arr[i].price, &arr[i].rooms,
&arr[i].bathroom, &arr[i].carpark, arr[i].type, &arr[i].area, arr[i].furnish);
The code above always outputs " 0 0 0 0 0". But when I try using a scanf and manually input one of the lines, it works perfectly.
The file it's reading from is a .csv file. Here is the contents:
Mont-Kiara;Kuala-Lumpur;1000000;2;2;0;Built-up;1000;Partly
Cheras;Kuala-Lumpur;310000;3;2;0;Built-up;1000;Partly
Kepong;Kuala-Lumpur;358000;3;3;0;Built-up;1000;Partly
Taman-Desa;Kuala-Lumpur;455000;2;2;0;Built-up;1000;Partly
Kepong;Kuala-Lumpur;358000;3;3;0;Built-up;1000;Partly
Kepong;Kuala-Lumpur;358000;3;3;0;Built-up;1000;Partly
And here is the full code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct houseData {
char loc1[101];
char loc2[101];
int price[101];
int rooms[101];
int bathroom[101];
int carpark[101];
char type[101];
int area[101];
char furnish[101];
} arr[1001];
int read() {
int i = 0;
struct houseData arr[800];
char temp1[100];
FILE *f = fopen("file.csv", "r");
while(!feof(f)){
fscanf(f, " %[^;];%[^;];%d;%d;%d;%d;%[^;];%d;%[^\n]"
, &arr[i].loc1, &arr[i].loc2, &arr[i].price, &arr[i].rooms,
&arr[i].bathroom, &arr[i].carpark, &arr[i].type, &arr[i].area, &arr[i].furnish);
i++;
}
fclose(f);
}
int main() {
read();
printf("%s %s %d %d %d %d %s %d %s", arr[i].loc1, arr[i].loc2, *arr[i].price, *arr[i].rooms, *arr[i].bathroom, *arr[i].carpark, arr[i].type, *arr[i].area, arr[i].furnish);
return 0;
}
There are a lot of issues in your code. Here's a version that may help. The error messages in this version are far from ideal (this does not distinguish between an input format error and a error reading data, for example, nor does it provide much detail on the location of the error), and there is still the possibility of undefined behavior on certain inputs, (see Is `scanf("%d", ...)` as bad as `gets`?) but this should point you in the right direction. Well, at least it may help to improve your use of scanf, but a very reasonable argument can the be made that the "right direction" is to stop using scanf completely. It is very difficult to get things right with scanf, and attempting to do so winds up being much more complex that just using fgets. But for simple use cases it is ... still pointless to use scanf. See http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html and many other resources that explain why scanf is a terrible choice.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct houseData{
char loc1[101];
char loc2[101];
int price;
int rooms;
int bathroom;
int carpark;
char type[101];
int area;
char furnish[101];
};
int
read(FILE * f, struct houseData *h)
{
return 9 == fscanf(f, " %100[^;]; %100[^;]; %d; %d; %d; %d; "
"%100[^;]; %d; %100[^\n]", h->loc1, h->loc2, &h->price,
&h->rooms, &h->bathroom, &h->carpark, h->type, &h->area,
h->furnish);
}
int
main(int argc, char **argv)
{
int rv;
FILE *f = argc > 1 ? fopen(argv[1], "r") : stdin;
if( f == NULL ){
perror(argv[1]);
return EXIT_FAILURE;
}
struct houseData h;
int i = 0;
while( read(f, &h) ){
printf("%d: %s %s %d %d %d %d %s %d %s\n", ++i,
h.loc1, h.loc2, h.price, h.rooms, h.bathroom,
h.carpark, h.type, h.area, h.furnish);
}
if( ! feof(f) ){
fprintf(stderr, "Error near line %d\n", i);
}
fclose(f);
}
#define max 5
typedef struct
{
char tar[20];
int trab[31];
}data;
int main()
{
int i,j, aux;
char fname[25];
data inf[max];
/*for(i=0;i<max;i++)
{
strcpy(inf[i].tar,inf[i-1].tar);
}*/
printf("File name?");
scanf(" %s", fname);
FILE *f=fopen("fname","r");
if(f== NULL)
{
printf("Cannot find\n");
return 1;
}
I try to read a file I'm very certain is in the same dir, however each time the FILE pointer returns NULL.
I would appreciate any help :(
Although you've tagged this as C++, your code looks more like C, so I'm using a C signature for main. The main problem you have is quotes around fname. Your code is ignoring the path that was entered and trying to open a file with the literal name fname.
int main(void)
{
int i,j, aux;
char fname[25];
printf("File name? ");
scanf(" %24s", fname); /* Always use width specifier on %s */
FILE *f=fopen(fname, "r"); /* No quotes around fname */
if( f == NULL ){
perror(fname);
return 1;
}
...
I trying to open a simple txt file in C, like the image bellow.
list example
The input text :
Name Sex Age Dad Mom
Gabriel M 58 George Claire
Louise F 44
Pablo M 19 David Maria
My doubt is, how can i make to identify the blank spaces in the list and jump correctly to another lines.
Here is my code:
#include <stdio.h>
int main() {
FILE *cfPtr;
if ((cfPtr = fopen("clients.txt", "r")) == NULL) {
puts("The file can't be open");
} else {
char name[20];
char sex[4];
int age;
char dad[20];
char mom[20];
char line[300];
printf("%-10s%-10s%-10s%-10s%-10s\n","Name","Sex","Age","Dad","Mom");
fgets(line,300,cfPtr);
fscanf(cfPtr,"%10s%10s%d%12s%12s",name,sex,&age,dad,mom);
while (!feof(cfPtr)) {
printf("%-10s%-10s%d%12s%12s\n",name,sex,age,dad,mom);
fscanf(cfPtr,"%19s%3s%d%12s%12s",name,sex,&age,dad,mom);
}
fclose(cfPtr);
}
return 0;
}
It works fine if I fill in all the spaces...
printf("%-10s%-10s%d%12s%12s\n",name,sex,age,dad,mom);
fscanf(cfPtr,"%19s%3s%d%12s%12s",name,sex,&age,dad,mom);
Change the order to read first, print later.
Ideally the data in your file should be separated by comma, tab, or some other character. If data is in fixed columns, then read everything as text (including integers) then convert the integer to text later.
Also check the return value for fscanf, if the result is not 5 then some fields were missing.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
FILE *cfPtr = fopen("clients.txt", "r");
if(cfPtr == NULL)
{
puts("The file can't be open");
return 0;
}
char name[11], sex[11], dad[11], mom[11], line[300];
int age;
fgets(line, sizeof(line), cfPtr); //skip the first line
while(fgets(line, sizeof(line), cfPtr))
{
if(5 == sscanf(line, "%10s%10s%10d%10s%10s", name, sex, &age, dad, mom))
printf("%s, %s, %d, %s, %s\n", name, sex, age, dad, mom);
}
fclose(cfPtr);
return 0;
}
Edit, changed sscan format to read integer directly, changed buffer allocation to 11 which is all that's needed.
This is for a hotel reservation system, that take a .txt file that contains lines of int string string int which it then reads and put into an array of type room... while scanning it keeps giving me segmentation fault... this is for class and i dont want a ready code to leech of but i just dont get why i keep getting segmentation... :/
#include <stdio.h>
#include <stdlib.h>
typedef struct{
int num;
char first[100];
char last[100];
int type;
}room;
int main (int argc, char ** argv){
FILE * myFile;
if(argc !=2)
{
fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
return EXIT_FAILURE;
}
myFile = fopen(argv[1],"r");
if (myFile==NULL){
fprintf(stderr, "no open!!\n");
return EXIT_FAILURE;
}
// counter to add elements to my array
int i = 0;
char c;
//array of room with the 150 rooms in it...
room * rooms = malloc(150 * sizeof(room));
while ((c = getc(myFile)) != EOF ){
fscanf(myFile, "%d", rooms[i].num);
printf("the room num is: %d", rooms[i].num);
fscanf(myFile, "%s", rooms[i].first);
fscanf(myFile, "%s", rooms[i].last);
fscanf(myFile, "%d", rooms[i].type);
i++;
}
fclose(myFile);
}
Here is what i fixed in my code and worked but it is literally skipping the fist integer in the .txt file that it reads from... it just reads a zero when it should be a 1 so i noticed that the "(c = getc(myFile)) != EOF" was my problem, it is skipping the first integer it is supposed to read :/
while ((c = getc(myFile)) != EOF ){
fscanf(myFile, "%d", &rooms[i].num);
fscanf(myFile, "%s", rooms[i].first);
fscanf(myFile, "%s", rooms[i].last);
fscanf(myFile, "%d", &rooms[i].type);
printf("the room num is: %d and is occupied by %s %s and it is a %d\n", rooms[i].num, rooms[i].first, rooms[i].last, rooms[i].type);
i++;
}
The .txt file's first line is as follows:
1 carri alston 0
In your code
fscanf(myFile, "%d", rooms[i].num);
should be
fscanf(myFile, "%d", &rooms[i].num);
same with the type thing.
Along with that, you should always check the return value of fscanf() to ensure proper scanning.
also, you need to put a check on the value of i so that it should not access out of bound memory.
I'm trying to open a file and pass to struct, I'm using fscanf() with a loop, but it only saves one the struct the last read:
Imagine a file:
JR John Rambo 24353432
JL John Lennon 6435463
I'm using this code:
typedef struct people{
char code[10];
char name[100];
long telephone;
}PEOPLE;
int read(PEOPLE people[], int n_p){
char temp;
FILE *fp;
fp=fopen("example.txt","r");
if(fp==NULL){
printf("Error\n");
return -1;
}
while(!feof(fp)){
fscanf(fp,"%s %s %s %d\n", people[n_p].code,people[n_p].name,
&people[n_p].telephone);
}
}
The Problem is that he only saves the last line of the file...Should I do a if cicle??
Another question is how can I separate a similar file but with ";"
First of all, you are scanning for 3 strings (%s) and one int (%d) when you pass only 3 parameters in your fscanf(). You could add a char first_name[50]; in your struct and then do:
fscanf(fp,"%s %s %s %d\n", people[n_p].code,people[n_p].first_name,
people[n_p].name, &people[n_p].telephone);
You always fscanf() the file until you have nothing more to read (due to the !feof(fp) because of the while. So in the last people[n_p] variable the last line of the file will be saved.
You could remove the while from read() and also add the FILE * as a parameter to the function so that you don't open the file each time you call read().
Something like this maybe:
main()
{
FILE* fp = fopen("example.txt", "r");
int i = 0;
while (!feof(fp)) {
read(people, i, fp);
i++;
}
}
int read(PEOPLE people[], int n_p, FILE* fp){
char temp;
if(fp==NULL){
printf("Error\n");
return -1;
}
fscanf(fp,"%s %s %s %d\n", people[n_p].code,people[n_p].first_name,
people[n_p].name, &people[n_p].telephone);
}
For using the ; as a separator you can change fscanf() to this:
fscanf(fp, "%[^;]; %[^;]; %d\n", people[n_p].code,people[n_p].name,
&people[n_p].telephone);
EDIT I wrote the above code which can be found here and it works fine with this example.txt file as input.
It looks like you're not changing the n_p variable. You need some sort of variable to keep track of which index of the people[] array you're updating.
Additionally, hopefully you've got a large enough people array to hold all the entries in the file.