Here is the website of the data files: Group Data Release
And here I am using the catalogs of SDSS_MTV (the third section from the bottom of the webpage).
The data are split into 8 parts. I am trying to read one of these files (such as "Fillingfactor_sdssMth12_80Mpc").
Actually, I am using fscanf() to get the data of "Fillingfactor_sdssMth12_80Mpc". Some of them show the values far greater than 1, that's unreasonable because the filing factor cannot exceed 1.
Here is the excerpt of this file (the first 5 rows):
609d 6c34 417e edb0 2664 0231 4452 3931
d948 2bb2 d877 15b2 0f5b e4b1 b646 9631
470e e3b1 06e2 3fb2 3483 f4b1 2c3d 11b2
6d86 89b1 8135 04b2 005c 44b2 d9eb a4b0
ad80 04b2 8dfb fcb1 03b0 3d32 afc6 4432
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main()
{
int i;
int Nx,Ny,Nz,itemp;
FILE *fp;
char filename[256];
sprintf(filename,"Fillingfactor_sdssMth12_80Mpc");
Nx=494;
Ny=892;
Nz=499;
float *quan = (float *)malloc(Nx*Ny*Nz*sizeof(float));
fp = fopen(filename,"r");
fread(&itemp,1,sizeof(float),fp);
// Checks the length of the table.
printf("itemp = %i\n", itemp);
if(itemp/sizeof(float) != Nx*Ny*Nz)
{
printf("error! itemp=%d\n",itemp);
exit(1);
}
for(i=0;i<Nx*Ny*Nz;i++)
{
fscanf(fp,"%f",&quan[i]);
printf("grid %i is: %.4lf\n", i, quan[i]);
}
fclose(fp);
}
Could someone explain me what is wrong?
Related
So I have to write a program that prompts the user to enter the name of a file, using a pointer to an array created in main, and then open it. On a separate function I have to take a user defined string to a file opened in main and return the number of lines in the file based on how many strings it reads in a loop and returns that value to the caller.
So for my first function this is what I have.
void getFileName(char* array1[MAX_WIDTH])
{
FILE* data;
char userIn[MAX_WIDTH];
printf("Enter filename: ");
fgets(userIn, MAX_WIDTH, stdin);
userIn[strlen(userIn) - 1] = 0;
data = fopen(userIn, "r");
fclose(data);
return;
}
For my second function I have this.
int getLineCount(FILE* data, int max)
{
int i = 0;
char *array1[MAX_WIDTH];
if(data != NULL)
{
while(fgets(*array1, MAX_WIDTH, data) != NULL)
{
i+=1;
}
}
printf("%d", i);
return i;
}
And in my main I have this.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_WIDTH 144
void getFileName(char* array1[MAX_WIDTH]);
int getLineCount(FILE* data, int max);
int main(void)
{
char *array1[MAX_WIDTH];
FILE* data = fopen(*array1, "r");
int max;
getFileName(array1);
getLineCount(data, max);
return 0;
}
My text file is this.
larry snedden 123 mocking bird lane
sponge bob 321 bikini bottom beach
mary fleece 978 pasture road
hairy whodunit 456 get out of here now lane
My issue is that everytime I run this I keep getting a 0 in return and I don't think that's what I'm supposed to be getting back. Also, in my second function I have no idea why I need int max in there but my teacher send I needed it, so if anyone can explain that, that'd be great. I really don't know what I'm doing wrong. I'll appreciate any help I can get.
There were a number of issues with the posted code. I've fixed the problems with the code and left some comments describing what I did. I do think that this code could benefit by some restructuring and renaming (e.g. array1 doesn't tell you what the purpose of the variable is). The getLineCount() function is broken for lines that exceed MAX_WIDTH and ought to be rewritten to count actual lines, not just calls to fgets.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_WIDTH 144
/**
* Gets a handle to the FILE to be processed.
* - Renamed to indicate what the function does
* - removed unnecessary parameter, and added return of FILE*
* - removed the fclose() call
* - added rudimentary error handling.
**/
FILE *getFile()
{
char userIn[MAX_WIDTH+1];
printf("Enter filename: ");
fgets(userIn, MAX_WIDTH, stdin);
userIn[strlen(userIn) - 1] = 0; // chop off newline.
FILE *data = fopen(userIn, "r");
if (data == NULL) {
perror(userIn);
}
return data;
}
/**
* - removed the unnecessary 'max' parameter
* - removed null check of FILE *, since this is now checked elsewhere.
* - adjusted size of array1 for safety.
**/
int getLineCount(FILE* data)
{
int i = 0;
char array1[MAX_WIDTH+1];
while(fgets(array1, MAX_WIDTH, data) != NULL)
{
i+=1;
}
return i;
}
/**
* - removed unnecessary array1 variable
* - removed fopen of uninitialized char array.
* - added some rudimentary error handling.
*/
int main(void)
{
FILE *data = getFile();
if (data != NULL) {
int lc = getLineCount(data);
fclose(data);
printf("%d\n", lc);
return 0;
}
return 1;
}
There are several things I think you should repair at first:
getFileName should help you getting the file name (as the name says), so in that function you shouldn’t have both array1 and userIn (as a matter of fact array1 is not even used in the function, so it can be eliminated all togheter). The paramater and the file name should be ‘the same’.
data is a local FILE pointer, this means once you exit the function you lose it. My recommandation is to make it global, or pass it as an argument from the main class. Also do not close it 1 line after you open it.
I guess the getLineCount is fine, but usually is a good practice to return and printf in main what is returned.
That max that is passed to the second function maybe to help you with the max size of a line? it might be.
Summing up, your getFileName should return the file name, so that userIn is what should be given by that parameter. The File opening should be done IN THE MAIN FUNCTION and be closed after everything you do related to the file, so at the end. Also, open the file after you get the name of the file.
Hopefully it helps you! Keep us tuned with your progress.
So currently I have this code that allows me to import words from a text file and store it in to an array, what I would like it to do is ignore any punctuation such as "," - "." - etc.
However I am unable to do so with %[,]s or something similar seen below, any help on this please?
#include <stdio.h>
#include <string.h>
int main()
{
FILE *myFileHandle;
myFileHandle = fopen("test1.txt", "r");
char *lineofText;
if (myFileHandle != NULL){
while (!feof(myFileHandle)){
fscanf(myFileHandle, "%[,]s", lineofText);
puts(lineofText);
}
}
fclose(myFileHandle);
}
Thanks.
#include <stdio.h>
#include <stdlib.h>
struct urunler {
int kod;
char Ad[16];
int stok;
float fiyat;
};
void urunTara(struct urunler* inputs,int *amount);
int main()
{
struct urunler Urun[50];
int amount = 0;
urunTara(Urun,&amount);
}
void urunTara(struct urunler *inputs,int *amount){
char Temp[150];
FILE *fPtr;
fPtr = fopen("urunler.txt","r");
if(fPtr == NULL){
printf("File not found!");
} else {
while(!feof(fPtr)){
fgets(Temp,100,fPtr);
sscanf(Temp,"%d %s %d %f",&(inputs[*amount].kod),inputs[*amount].Ad,&(inputs[*amount].stok),&(inputs[*amount].fiyat));
*amount++;
}
}
};
I am relatively new to C, and just started learning about structs. The text file contains these:
25 televizyon 1000 150.25
40 video 500 25.45
50 plazma 75 2300.50
76 dvd 20000 90.00
85 supurge 700 110.75
90 buzluk 250 10.00
95 teyp 1250 8.99
The problem i have here is with the sscanf. When i do all these inside the main function, it works great. However when i try to do it in the function urunTara something goes wrong with the sscanf statement and the program stops working. I successfully passed values to &(inputs[*amount].kod) and other adresses by using scanf. But can't understand what's the problem with this sscanf statement.
*amount++;
is similar to
*(amount++);
which means dereference amount and then increment amount to the element after that. Which is not correct.
Change *amount++; to (*amount)++;, which will increment the dereferenced value.
Refer this and this for more details
So i have a file with multiple lines on it, and I want to copy some of those line to a new file, random lines to be specific.
Everything goes fine as long as I copy all the lines file from file, but when I try to introduce randoming, in the new file the lines misses couple characters
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <malloc.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
srandom(time(NULL));
int n,i=0,j=0,k,l;
char c[200][200];
char *buff=(char *)malloc(sizeof(char));
int f= open("erzekelokbeugro.txt",O_RDONLY);
int f2= open("teszt.txt",O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
do
{
do
{
n=read(f,buff,1);
c[j][i]=*buff;
//printf("%c",buff[0]);
i++;
}
while(buff[0] !='\n');
j++;
}
while(n>0);
k=10;
//j=73;
i=0;
while(k>0)
{
j=rand()%49;
//printf("%d ", j);
do
{
*buff=c[j][i];
if(buff[0]!=0)
{
write(f2,buff,1);
//printf("%c",buff[0]);
}
i++;
}
while(buff[0] !='\n');
//printf("----\n");
k--;
}
close(f);
close(f2);
return 0;
}
The input is someting like this (i wont copy the whole file, just a sample, its in hungarian, but the content doesnt matter anyway):
8.Érzékelés alsó határa (detektálási küszöb)
9.Felbontás
10.Nullponteltolódás (drifft)
11.Érzékenység eltolódás
12.Ismétlési (reprodukciós) hiba
13.Csereszabatosság mértéke
14.Szelektivitás
15.Beállási idő
16.Élettartam
17.Hőmérséklet definíciója
18.Mit tud a Celsius skáláról:
19.Ismertesse a hőmérsékleti együttható (?) képletét az ellenállás-hőmérő esetén.Nevezze meg a képlet elemeit.
and the output is like this:
és voltmérő sematikus ábrája
5.Linearitás
as műszerek működési elve:
15.Beállási idő
26.Ismertesse a nyomás definícióját, és kiszámítási képletét.
28.Villamos elvű passzív nyomásmérő főbb típusai
34.Ellenállás nyomásérzékelő mérési tartománya és pontossága
29.A villamos elvű nyomásmérők előnyei
35.Piezoelektromos nyomásérzékelés mérési elve
ust használó szenzorok előnyei:
As you can see, all the line should start with a number, but sometimes its missing, also some character (even half a line), but the next goes fine
i needs to be initialized each time you pick a new j. You might also want to look into how to print a whole string at once, instead of doing it a character at a time.
Hello i have a text file that contains cities, dates and temperature. How can i read temperature from specific city and day? For example if I search for Alingsås 2014-05-14 I need to get 13.81, 11.59 and 13.81. The part I am stuck with is after i have opened the file and put variables for the city and date. Info is stored in the text file like this:
Alingsås;
2014-05-14;
13.81;
11.59;
13.81;
2014-05-15;
8.89;
7.99;
9.15;
2014-05-16;
6.2;
5.07;
6.58;
2014-05-17;
7.91;
5.55;
7.91;
2014-05-18;
7.76;
5.95;
7.76;
2014-05-19;
7.95;
6.91;
9.72;
2014-05-20;
18.45;
12.92;
18.45;
Arboka;
2014-05-14;
9.55;
4.53;
10.66;
2014-05-15;
6.33;
1.5;
9.37;
2014-05-16;
8.85;
3.4;
12.08;
2014-05-17;
14.01;
4.8;
15.4;
2014-05-18;
14.16;
6.35;
17.23;
2014-05-19;
21.39;
14.57;
21.39;
2014-05-20;
23.34;
14.82;
23.34;
My Code so far is
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <locale.h>
#include <string.h>
int main(void)
{
FILE * fp;
char c;
// open file
fp =fopen("C:\\Users\\Karl\\Desktop\\info.txt", "r");
if (fp != NULL)
{ char city[10] = "Ålingsås";
int temp1;
int temp2;
int temp3;
char date[13] = "2014-05-14";
One solution is to make a structure that contains the name of the city, and a pointer to another structure which will be an "array" (dynamically allocated on the heap) and where each entry contains the data and the three temperatures.
Then read one line at a time from the file, trim the input (to remove the leading and trailing whitespace, if there is any) and create the above structure. If the current line starts with a digit it's a date and read three more lines for the temperature and fill in the structure. If the current line doesn't start with a digit then you have a new city, and you create a new city-structure.