I am trying to get the data from quiz_scores.txt to print to the screen so I can scan it into an array but I don't know how. Do I need to hard-code the file into my program and if so how?
#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
int main(void)
{
//initializing variables
FILE *es;
FILE *hs;
int num=0;
int i;
int j;
int cols=10;
int rows=10;
int qid, s;
int quizarray[0][0];
int MAX_LENGTH=15;
char *result0;
char line[MAX_LENGTH];
FILE *qs="C:\\quiz_scores.txt";
qs = fopen("quiz_scores.txt", "r");
/*for (i=0; i<cols; i++){
for (j=0; j<rows; j++){
quizarray[i][j]=0;
fscanf(qs, "%d%d", quizarray[i][j]);
}
}
*/
while(fgets(line, MAX_LENGTH, qs))
{
printf("%s", line);
}
if(qs == NULL)
{
printf("Error: Could not open file\n");
return -1;
}
/*for (i=0;i<n;i++)
{
fprintf(qs, "%d\n");
}
fclose(qs);*/
return 0;
}
OK I think I know what you want to happen, read numbers from the file quiz_scores.txt into your 2d array and print them,correct?. I hope these corrections will allow you to do just that and also that you will understand why it wouldn't work before. The rest of the program is for you to do yourself, good luck!!
#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
int main(void)
{
//initializing variables
int num=0,i,j,cols=10,rows=10;//just smooshed all onto the same line cause it was annoying me
int quizarray[10][10];//You declared a 2d array with 0 elements(you wanted one with ten rows and columns I presume 10 teams 10 rounds)
FILE *qs = fopen("quiz_scores.txt", "r");//Your declaration of the file pointer was incorrect
if(qs == NULL)//This if needs to go before you read anything from the file otherwise its kind of redundant
{
printf("Error: Could not open file\n");
return -1;
}
for (i=0; i<cols; i++)
{
for (j=0; j<rows; j++)
{
quizarray[i][j]=0;
fscanf(qs,"%d", &quizarray[i][j]);//Them scanfs need &s mate
\\and don't try to scan in two integer into one array element
printf("%d",quizarray[i][j]);
if(j<rows-1)
printf("-");
}
printf("\n");
}
fclose(qs);//Dont comment fclose out bad things will happen :P
return 0;
}
Related
The wordlist.txt is including like:
able
army
bird
boring
sing
song
And I want to use fscanf() to read this txt file line by line and store them into a string array by indexed every word like this:
src = [able army bird boring sing song]
where src[0]= "able", src[1] = "army" and so on. But my code only outputs src[0] = "a", src[1] = "b"... Could someone help me figure out what's going wrong in my code:
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
FILE *fp = fopen("wordlist.txt", "r");
if (fp == NULL)
{
printf("%s", "File open error");
return 0;
}
char src[1000];
for (int i = 0; i < sizeof(src); i++)
{
fscanf(fp, "%[^EOF]", &src[i]);
}
fclose(fp);
printf("%c", src[0]);
getchar();
return 0;
}
Pretty appreciated!
For example like this.
#include <stdio.h>
#include <string.h>
#include <errno.h>
#define MAX_ARRAY_SIZE 1000
#define MAX_STRING_SIZE 100
int main(int argc, char *argv[]) {
FILE *fp = fopen("wordlist.txt", "r");
if (fp == NULL) {
printf("File open error\n");
return 1;
}
char arr[MAX_ARRAY_SIZE][MAX_STRING_SIZE];
int index = 0;
while (1) {
int ret = fscanf(fp, "%s", arr[index]);
if (ret == EOF) break;
++index;
if (index == MAX_ARRAY_SIZE) break;
}
fclose(fp);
for (int i = 0; i < index; ++i) {
printf("%s\n", arr[i]);
}
getchar();
return 0;
}
Some notes:
If there is an error, it is better to return 1 and not 0, for 0 means successful execution.
For a char array, you use a pointer. For a string array, you use a double pointer. A bit tricky to get used to them, but they are handy.
Also, a check of the return value of the fscanf would be great.
For fixed size arrays, it is useful to define the sizes using #define so that it is easier to change later if you use it multiple times in the code.
It's reading file one character at a time, Which itself is 4 in size like we see sizeof('a') in word able. Same goes for 'b' and so on. So one approach you can use is to keep checking when there is a space or newline character so that we can save the data before these two things as a word and then combine these small arrays by adding spaces in between and concatenating them to get a single array.
I'm working on a task where I need to read a long .txt file. The first line contains the number of lines the .txt file has, the rest of the lines follow the same structure, "int int int char:char".
How do I read the first line separately from the rest?
I wrote the following code:
FILE *fajl;
falj = ("musor.txt", "r");
while (!feof(fajl) && fajl > 1)
{
fscanf_s(fajl, "%d %d %d %[^:]c:%c\n", &tomb[i].ado, &tomb[i].perc, &tomb[i].masodperc, &tomb[i].eloado, &tomb[i].cim);
i++;
}
Sorry for the unknown words, the variable names are in Hungarian.
So this is basically just the collection of all the comments from the question.
//-------------------------
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct Tomb
{
int masodperc, eloado, ado, perc;
char cim;
};
typedef struct Tomb Tomb;
//-------------------------
int main(int argc, char *argv[])
{
char linesLength[10], stringFajl[100];
FILE *fajl;
fajl = fopen("musor.txt", "r");
if(fgets(linesLength, 100, fajl)==NULL || fajl == NULL) //first line read
{
printf("error\n");
return -1;
}
int length = atoi(linesLength), i=1;
Tomb tomb[length];
while (fgets(stringFajl, 100 ,fajl )!=NULL || i<=length)
{
sscanf(stringFajl, "%d %d %d %d [^:]c:%c", &tomb[i].ado, &tomb[i].perc, &tomb[i].masodperc, &tomb[i].eloado, tomb[i].cim);
i++;
}
fclose(fajl);
return 0;
}
I am trying to write a program for some classwork that reads in a file using fscanf, and then stores each word into an array with one word in each element. Then I need to print each element of the array out on to a new line on the console.
The getty.txt file has the Gettysburg address in it with appropriate spacing, punctuation, and is multiline.
What I think is happening is that the entire text is being stored in the first element of the array, but I am not 100% sure as I am still learning to debug and write in C.
Any advice as to what I am doing wrong would be great! I currently only seem to be getting the last word and some extra characters.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void readFile();
void writeFile(char* buffer, int bufferlen);
FILE *fpread;
FILE *fpwrite;
char filebuffer[1000];
int filebufferlen = 0;
int main(int argc, char *argv[]) {
fpwrite = fopen("csis.txt", "w");
readFile();
writeFile(filebuffer, filebufferlen);
fclose(fpwrite);
return 0;
}
void readFile() {
char c;
filebufferlen = 0;
if(!(fpread = fopen("getty.txt", "r"))){
printf("File %s could not be opened. \n", "getty.txt");
fprintf(fpwrite,"File %s could not be opened. \n", "getty.txt");
exit(1);
}
while (!feof(fpread)) {
fscanf(fpread, "%s", filebuffer);
filebufferlen++;
}
}
void writeFile(char* filebuffer, int filebufferlen) {
for (int i = 0; i < filebufferlen; ++i){
printf("%c\n", filebuffer[i]);
}
}
After fixing the compile problems:
the code does not contain any code nor data declarations to contain an array of 'words.' So naturally, nothing but the last word is actually saved so it can be printed out.
I wrote this code to read in the following line from a text file:
1019*/passed// 56.)100,/ 82//10 however when I print it afetr the sscanf function I only get the 1019 and the number 10 correctly , rest have garbage values , any idea why? I'd appreciate if someone could correct me, I even tried putting %d instead of just %[..] in the sscanf function but stil doesnt print out the desired values.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ()
{
char buffer[100];
char status[20];
int grades[4]= {0};
int id;
int j;
int k;
int temp;
FILE *pointer = fopen ("testing5.txt", "r");
if((pointer = fopen("testing5.txt", "r"))!= NULL)
{
printf("No error, file successfully opened\n you may contine :)\n");
}
else
{printf("file could not be opened\nterminating program,...\n");
return 0;}
fgets(buffer,100,pointer);
printf("%s\n", buffer);
sscanf(buffer,"%[^*/]*/%[^//]//%[^.).)]%[^,/],/ %[^//]//%d",&id,status, &grades[0],&grades[1],&grades[2],&grades[3]);
printf("%d\n%s\n%d\n%d\n%d\n%d",id,status, grades[0],grades[1],grades[2],grades[3]);
for (j =0; j<3; j++)
for(k =j+1; k<4;k++)
{
if(grades[j]<grades[k] )
{
temp = grades[j];
grades[j] = grades[k];
grades[k] = temp;
}
}
printf("\nthe grades in ascending order is \n");
for (j =0; j<4; j++)
{
printf("%d\n", grades[j]);
}
return 0;
}
sscanf("1019*/passed// 56.)100,/ 82//10\n", "%d*/%[^/]// %d.)%d,/ %d//%d",
&id, status, &grades[0],&grades[1],&grades[2],&grades[3]);
Change
int grades[4]= {0}
to
char grades[4] = { 0 };
The warning output is pointing out the problem, don't ignore it. You are passing the wrong data types to sscanf.
Hey there, How do I go about copying text inside a text file into a multidimensional character array?
supposing the text file( text_file.txt) contained
this is the first line
this is the second line
this is the third line
#include <stdio.h>
int main(void){
FILE *f;
f=fopen("text_file.txt","r");
if (f==NULL){
printf("invalid!");
return 1;
}
else {
printf("successful");
}
char copied_text[80][80];
while (!feof(f)){
int i=0,j=0;
fgets(copied_text[i][j],"%s",f);
i++;
}
return 0;
}
-thank you.
I think your code almost work.
Just move the declaration of int i out of the loop.
Change the first parameter of fgets to copied_text[i] because it needs a pointer here.
Change the second parameter of fgets to 80 because it should be a int indicates the acceptable string length.
#include <stdio.h>
int main(void){
FILE *f;
f=fopen("text_file.txt","r");
if (f==NULL){
printf("invalid!\n");
return 1;
}
else {
printf("successful\n");
}
char copied_text[80][80];
int i=0;
while (!feof(f)){
fgets(copied_text[i],80,f);
++i;
}
for(int i = 0; i <3; ++i)
printf("%s\n", copied_text[i]);
return 0;
}